判断用户是否在线的代码

%mytime=rs(“time”卡塔尔国’数据库中保留时间nowtime=now(卡塔尔(قطر‎’当前时刻ifdatediff(“d”,mytime,nowtime卡塔尔国1then’相比当前光阴和数据库中时间,如相差小于1,即展现图片new.gif%imgsrc=”images/new.gif”%endif%

此地要细心的是js的日子戳是16个人,php的时光戳是十人,调换函数如下:

设想二种情形:
(1)顾客关闭浏览器或重定向到其余网页

复制代码 代码如下:

复制代码 代码如下:

var nowtime = (new Date).getTime();/*一时一刻时刻戳*/
/*改变时间,计算差值*/
function comptime(beginTime,endTime){
var secondNum =
parseInt((endTime-beginTime*1000卡塔尔国/1000卡塔尔(قطر‎;//总计时间戳差值
if(secondNum>=0&&secondNum<60){
return secondNum+’秒前’;
}
else if (secondNum>=60&&secondNum<3600){
var nTime=parseInt(secondNum/60);
return nTime+’分钟前’;
}
else if (secondNum>=3600&&secondNum<3600*24){
var nTime=parseInt(secondNum/3600);
return nTime+’小时前’;
}
else{
var nTime = parseInt(secondNum/86400);
return nTime+’天前’;
}
}
t =
comptime(“1324362556”,nowtime卡塔尔;//timestamp为PHP通过ajax回传的时辰戳
alert(t);

<script type=text/javascript>
function exit_init() {
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){ //
}
else{
alert(“there was a problem accessing the server:”+xmlhttp.status);
}
}
}
//定义windows 的onbeforeunload
事件,当客户非符合规律退出即浏览器非常常关闭时,对客户登入情状实行管理
window.onbeforeunload=function () {
//if(event.clientY<0||event.altKey) {
exit_request = false;
//创建需要对象
if (window.XMLHttpRequest) {
exit_request = new XMLHttpRequest();
if (exit_request.overrideMimeType){
exit_request.overrideMimeType(‘text/xml’);
}
} else if (window.ActiveXObject) {
try{
exit_request = new ActiveXObject(“Msxml2.XMLHTTP”);
} catch (e) {
try {
exit_request = new ActiveXObject(“Microsoft.XMLHTTP”);
} catch (e) {
}
}
}
if (!exit_request) {
alert(“Your brower is not compatible the current opration.Please use the
IE 5.0! “);
return false;
}
var url=’null.php?userid=’+document.getElementById(“userid”).value;
//定义页面调用的方法exit_init,不是exit_init();没有();
exit_request.onreadystatechange = exit_init;
exit_request.open(‘GET’, url, true);
//禁止IE 缓存
exit_request.setRequestHeader(“If-Modified-Since”,”0″);
//发送数据
exit_request.send(null);
}
//}
</script>

复制代码 代码如下: var nowtime = (new DateState of Qatar.getTime(卡塔尔(قطر‎;/*最近时间戳*/
/*转换时…

证实:null.php用来将客户情形设为下线
(2)顾客长日子不再浏览本网址
思路:创建表active_stat,属性有userid,lasttime,nowtime。客商每打开三遍本网址,修改lasttime为近日不久子now(State of Qatar,每间距1秒钟校勘nowtime为当前时光,判别nowtime-lasttime是或不是高于20分钟,若当先,则修正用户景况为下线

复制代码 代码如下:

<script language=javascript>
function test(userid){
setInterval(“offline(‘”+userid+”‘卡塔尔”, 60000 卡塔尔(قطر‎; //每间距1分钟履行贰回
}
function offline(userid){
var xmlhttp=false;
try{
xmlhttp=new activeXObject(‘Msxml2.XMLHTTP’);
}catch(e){
try{
xmlhttp=new ActiveXObject(‘Microsoft.XMLHTTP’);
}catch(e){
try{
xmlhttp=new XMLHttpRequest();
}catch(e){}
}
}
if(xmlhttp.readyState==4||xmlhttp.readyState==0)
{
xmlhttp.open(‘get’,’../user/include/offline.php?userid=’+userid,false);
xmlhttp.onreadystatechange=function(){
if(xmlhttp.readyState==4){
if(xmlhttp.status==200){
//
}
else{ alert(“there was a problem accessing the
server:”+xmlhttp.status);}
}
}
xmlhttp.send(null);
}
}
</script>
<BODY onLoad=”test(‘<? echo $userid;?>’);”>

offline.php

复制代码 代码如下:

<?php
//连接数据库
$userid=$_GET[“userid”];
pg_exec(“update active_stat set nowtime=now() where
userid='”.$userid.”‘;”);
$result=pg_exec($dbconn,”select (nowtime-lasttime) as activetime from
active_stat where userid='”.$userid.”‘”);
$str=pg_result($result,0,”activetime”);
if(strlen($str)>16){
$array=explode(” “,$str);
$t=explode(“:”,$array[1]);
$t[0]=$t[0]+$array[0]*24;
$s=explode(“.”,$t[2]);
$t[2]=$s[0];
$y=((int)$t[0])*3600+((int)$t[1])*60+((int)$t[2]);
}
else{
$total_time=explode(“.”,$str);
$x=explode(“:”,$total_time[0]);
$y=((int)$x[0])*3600+((int)$x[1])*60+((int)$x[2]);
}
if($y>=1200){
pg_exec(“update users set status=’f’ where userid='”.$userid.”‘;”);
}
?>

(1)客商关闭浏览器或重定向到别的网页 复制代码 代码如下: script
type=text/javascript function exit_init() {
if(xmlhttp.readyState==4)…

You can leave a response, or trackback from your own site.

Leave a Reply

网站地图xml地图