判斷用戶是否在線的代碼
更新時(shí)間:2011年03月05日 12:16:05 作者:
判斷用戶是否在線的代碼,需要的朋友可以參考下。
考慮兩種情況:
(1)用戶關(guān)閉瀏覽器或重定向到其他網(wǎng)頁
<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 事件,當(dāng)用戶非正常退出即瀏覽器非正常關(guān)閉時(shí),對(duì)用戶登錄狀態(tài)進(jìn)行處理
window.onbeforeunload=function () {
//if(event.clientY<0||event.altKey) {
exit_request = false;
//創(chuàng)建請(qǐng)求對(duì)象
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;
//定義頁面調(diào)用的方法exit_init,不是exit_init();沒有();
exit_request.onreadystatechange = exit_init;
exit_request.open('GET', url, true);
//禁止IE 緩存
exit_request.setRequestHeader("If-Modified-Since","0");
//發(fā)送數(shù)據(jù)
exit_request.send(null);
}
//}
</script>
說明:null.php用來將用戶狀態(tài)設(shè)為下線
(2)用戶長(zhǎng)時(shí)間不再瀏覽本網(wǎng)站
思路:創(chuàng)建表active_stat,屬性有userid,lasttime,nowtime。用戶每打開一次本網(wǎng)站,更改lasttime為當(dāng)前時(shí)間now(),每隔1分鐘更改nowtime為當(dāng)前時(shí)間,判斷nowtime-lasttime是否大于20分鐘,若大于,則修改用戶狀態(tài)為下線
<script language=javascript>
function test(userid){
setInterval("offline('"+userid+"')", 60000 ); //每隔1分鐘執(zhí)行一次
}
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
//連接數(shù)據(jù)庫
$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)用戶關(guān)閉瀏覽器或重定向到其他網(wǎng)頁
復(fù)制代碼 代碼如下:
<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 事件,當(dāng)用戶非正常退出即瀏覽器非正常關(guān)閉時(shí),對(duì)用戶登錄狀態(tài)進(jìn)行處理
window.onbeforeunload=function () {
//if(event.clientY<0||event.altKey) {
exit_request = false;
//創(chuàng)建請(qǐng)求對(duì)象
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;
//定義頁面調(diào)用的方法exit_init,不是exit_init();沒有();
exit_request.onreadystatechange = exit_init;
exit_request.open('GET', url, true);
//禁止IE 緩存
exit_request.setRequestHeader("If-Modified-Since","0");
//發(fā)送數(shù)據(jù)
exit_request.send(null);
}
//}
</script>
說明:null.php用來將用戶狀態(tài)設(shè)為下線
(2)用戶長(zhǎng)時(shí)間不再瀏覽本網(wǎng)站
思路:創(chuàng)建表active_stat,屬性有userid,lasttime,nowtime。用戶每打開一次本網(wǎng)站,更改lasttime為當(dāng)前時(shí)間now(),每隔1分鐘更改nowtime為當(dāng)前時(shí)間,判斷nowtime-lasttime是否大于20分鐘,若大于,則修改用戶狀態(tài)為下線
復(fù)制代碼 代碼如下:
<script language=javascript>
function test(userid){
setInterval("offline('"+userid+"')", 60000 ); //每隔1分鐘執(zhí)行一次
}
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
復(fù)制代碼 代碼如下:
<?php
//連接數(shù)據(jù)庫
$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."';");
}
?>
相關(guān)文章
easyui window refresh 刷新兩次的解決方法(推薦)
下面小編就為大家?guī)硪黄猠asyui window refresh 刷新兩次的解決方法(推薦)。小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-05-05
google廣告之另類js調(diào)用實(shí)現(xiàn)代碼
這篇文章主要介紹了google廣告之另類js調(diào)用實(shí)現(xiàn)代碼,需要的朋友可以參考下2020-08-08
javascript輕量級(jí)模板引擎juicer使用指南
Juicer 是一個(gè)高效、輕量的前端 (Javascript) 模板引擎,使用 Juicer 可以是你的代碼實(shí)現(xiàn)數(shù)據(jù)和視圖模型的分離(MVC)。2014-06-06
JavaScript 未結(jié)束的字符串常量常見解決方法
做JavaScript的時(shí)候,發(fā)現(xiàn)老是出現(xiàn)錯(cuò)誤:“未結(jié)束的字符串常量”. 自己找了下應(yīng)該是傳參數(shù)的時(shí)候,有特殊字符引起的.網(wǎng)上也找了下,也有好多出現(xiàn)這種情況.做下總結(jié),以方便以后查閱.2010-01-01
微信小程序?qū)崿F(xiàn)橫向滾動(dòng)導(dǎo)航欄效果
這篇文章主要介紹了微信小程序?qū)崿F(xiàn)橫向滾動(dòng)導(dǎo)航欄效果,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2019-12-12

