考虑两种情况:
(1)用户关闭浏览器或重定向到其他网页
复制代码 代码如下:
<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>

说明:null.php用来将用户状态设为下线
(2)用户长时间不再浏览本网站
思路:创建表active_stat,属性有userid,lasttime,nowtime。用户每打开一次本网站,更改lasttime为当前时间now(),每隔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."';");
}
?>
标签:
用户,是否在线

免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
评论“判断用户是否在线的代码”
暂无“判断用户是否在线的代码”评论...

P70系列延期,华为新旗舰将在下月发布

3月20日消息,近期博主@数码闲聊站 透露,原定三月份发布的华为新旗舰P70系列延期发布,预计4月份上市。

而博主@定焦数码 爆料,华为的P70系列在定位上已经超过了Mate60,成为了重要的旗舰系列之一。它肩负着重返影像领域顶尖的使命。那么这次P70会带来哪些令人惊艳的创新呢?

根据目前爆料的消息来看,华为P70系列将推出三个版本,其中P70和P70 Pro采用了三角形的摄像头模组设计,而P70 Art则采用了与上一代P60 Art相似的不规则形状设计。这样的外观是否好看见仁见智,但辨识度绝对拉满。