网管联盟 | 网管论坛 | 网管u家 | 网管博客 | 网管软件 | 网管求职 | 小游戏 | 网管搜索 | 网管原创 | 网管聚合 | 网管读摘 | 网管焦点 | 世界素材 | 会员投稿 | 会员中心 
中国网管联盟
Windows Linux Cisco 网络技术 数据库 黑客攻防 DotNet Java PHP 认证 新闻资讯 服务器 存储资讯 网络设备 网管学堂 技术专题 焦点 网吧频道
 当前位置: > bitsCN.com > PHP > PHP入门安装 > 判断在线用户的方法  

判断在线用户的方法

2006-02-27  作者:bitsCN整理  来源:中国网管联盟  点评 投稿 收藏

解决了昨天的问题:表结果变了一下,如下:
CREATE TABLE TB_User (       --用户表
N_UserId   Number(5)      NOT NULL,       --用户ID
V_NickName  VARCHAR2(10)  NOT NULL,       --昵   称
V_PWD      VARCHAR2(10)  NOT NULL,        --密   码
V_TrueName  VARCHAR2(20),                 --姓   名
Primary Key (N_UserId)
)
CREATE TABLE TB_OnlineUser ( --在线用户
N_OnlineUserId Number(5)      NOT NULL,   --在线用户ID
     D_LoginTime   Number (16),       --登陆时间以秒计 网管u家u.bitscn@com
     N_OnlineID    Number(5),   --与onlineusercount相关联。
    Primary Key (N_OnlineID)
)
/
CREATE TABLE TB_OnlineUserCount (  --在线用户统计表
N_OnlineID    Number(5)      NOT NULL,   --系统ID号
N_OnlineUserId Number(5)      NOT NULL,   --在线用户ID
D_LoginDate    Date                   ,          --登陆日期
D_LoginTime   Number (16)    ,    --登陆时间以秒计
D_OverDate      Date    ,          --结束日期
D_OverTime    Number (16)             ,    --结束时间
网管网www_bitscn_com

     Primary Key (N_OnlineID)
)
/

/*---LoginselectNew.php---该程序是登陆检查程序----*/
<?
session_start();
/*思路:首先用户登陆,判断是否有该用户,判断是否密码通过,否则返回参数进行特殊处理。(登陆不成功)
  登陆成功后,如果该用户不在线(一般不在线,特殊情况如果他用另一台机器打开浏览器重新再登陆,那么他有可能在线),
  先进行session变量注册,取得相应条件向1.统计表与2.在线表中插数据。进入到登陆页。
  如果用户在线:先取得在线用户的系统ID,因为在备份该用户离开时有用。接着删除该在线用户.接着进行该用户离开时间的备份.
*/
session_register("objsNickName");
require('oracle8conn.php');
$name=trim($name);
$pwd=trim($pwd);
ob_start();      //缓冲输出
$stmtNick = OCIParse($conn,"select count(*) countnickname from tb_user where v_nickname='$name'");
OCIExecute($stmtNick);
   while(OCIFetchInto($stmtNick,&$arrN)){ 网管u家u.bitscn@com
     if ($arrN[0]==0){
           Header("Location:Logintest.php?Msg=1");
     }else{
           //用户名通过
           unset($arrNickName);           //撤消临时数组
           $stmtPwd = OCIParse($conn,"select count(*) countpwd from tb_user where v_pwd='$pwd' and v_nickname='$name'");
           OCIExecute($stmtPwd);
            while(OCIFetchInto($stmtPwd,&$arrP,OCI_NUM)){
              if ($arrP[0]==0){
                 Header("Location:Logintest.php?Msg=2"); 网管联盟bitsCN_com
            }else{//密码通过
    //取出用户的ID号
       $stmtUid = OCIParse($conn,"select n_userID from tb_user where v_nickname='$name'");
       OCIExecute($stmtUid);
       while(OCIFetchInto($stmtUid,&$arrU,OCI_NUM)){
            $intOnlineUserID=$arrU[0];          
       }//while_Over    
    //如果该用户通过另一个浏览器重复登陆,解决如下
       $stmOnlineFlag=OCIParse($conn,"select count(*) from tb_onlineuser where N_ONLINEUSERID='$intOnlineUserID'");
       OCIExecute($stmOnlineFlag);     
中国网管论坛bbs.bitsCN.com

       while(OCIFetchInto($stmOnlineFlag,&$arronlineFlag,OCI_NUM)){
              if ($arronlineFlag[0]!=0){                               //表示已经在线
                                                                      //先取到在线用户关联系统ID
                  $stmtSysID= OCIParse($conn,"select N_ONLINEID from tb_onlineuser where N_ONLINEUSERID='$intOnlineUserID'"); 网管朋友网www_bitscn_net
                  OCIExecute($stmtSysID);
                  while(OCIFetchInto($stmtSysID,&$arrSysID,OCI_NUM)){
                      $SysID=$arrSysID[0];          
                   }//while_Over                                     //找完后踢出该用户
                  $stmt = OCIParse($conn, "delete from tb_onlineuser where N_ONLINEUSERID='$intOnlineUserID'");
网管u家bitscn.net

                  OCIExecute($stmt);
                  print "删除成功";                                  //最后作记录备份
                  $tmpTime=time(); //结束时间
                  $DatLoginDate = date( "Y-m-d");//结束日期
                  $DatLoginDate = "to_date('".$DatLoginDate."','YY/MM/DD')";
                  $stmtUserCount = OCIParse($conn, "update tb_onlineusercount set D_OverDate=$DatLoginDate ,D_OverTime=$tmpTime where N_OnlineID='$SysID'");//条件是相关联的系统ID
网管有家bitscn.net

                  OCIExecute($stmtUserCount);
                  print "添加成功到统计表中。";
                }//endif                                              //不在线正常注册
       $objsNickName=$name; //注册Session变量
       unset($arrPwd);             //撤消临时数组
       srand((double)microtime()*1000000000); 网管u家www.bitscn.net
       $intOnlineID = rand();              //取一个系统ID号
       $DatLoginDate = date( "Y-m-d");    //取得系统日期存入到Online表中去。
       $DatLogintime = time();           //取系统时间
       $DatLoginDate = "to_date('".$DatLoginDate."','YY/MM/DD')";    
       $stmt = OCIParse($conn, "insert into tb_onlineuser (N_OnlineUserId,D_LoginTime,N_OnlineID) values ($intOnlineUserID,$DatLogintime,$intOnlineID)");
       OCIExecute($stmt);
       $stmtC = OCIParse($conn, "insert into TB_OnlineUserCount (N_OnlineID,N_OnlineUserId,D_LoginDate,D_LoginTime) values ($intOnlineID,$intOnlineUserID,$DatLoginDate,$DatLogintime)");

网管u家u.bitsCN.com


       OCIExecute($stmtC);
       Header("Location:index.php");  //成功登陆!
          }//whileOVER
        }//end if
      }//while_Over
    }//end if
}//while_Over

?>
<?ob_end_flush();?>
/*-------CheckSession-----检查刷新程序---*/
<?
/*30分钟刷新程序
  先统计出在线的用户数,如果没有在线用户,系统要保证一个系统指定用户。该系统用户时时在线的原因是保证该刷新程序的执行
  如果该登陆用户Session不存在了,表示用该用户离线。统计出时间。
*/
  session_start();
  require('oracle8conn.php');
  print $objsNickName;
?>
  <html><head><meta HTTP-EQUIV=refresh Content='1800;url="CheckSession.php"'> 网管有家bitscn.net
<?
$NowDate = date("Y-m-d");
$NowDate = "to_date('".$NowDate."','YY/MM/DD')";
$NowTime = time();
//统计在线人数。30分钟更新一次
$stmtCount = OCIParse($conn,"select count(*) from tb_onlineuser");
OCIExecute($stmtCount);
while(OCIFetchInto($stmtCount,&$arrCountUser)){
$CountUser=$arrCountUser[0];
}
print "目前在线人数为:".$CountUser."<br>";
//判断在线否?
if ($CountUser==0){
   print "没有人在线!特殊处理!";
}else{
$stmtOnlineUser = OCIParse($conn,"select N_OnlineUserId,D_LoginTime,N_OnlineID from tb_onlineuser");
OCIExecute($stmtOnlineUser);
$arrTest = array();
while(OCIFetchInto($stmtOnlineUser,&$arrUser[])){
    $arrTest += $arrUser;
}
$j = sizeof($arrTest);
if($j>0){
    $i = sizeof($arrTest[0]);     
网管有家bitscn.net

  }
}    
   for($b=0;$b<$j;$b++){  //因为存入二维数组中,所以双重循环。
    for($a=0;$a<1;$a++){ //内循环一次找到时间。
    //注意双循环中是为了取数组值
    // $arrTest[$b][0]  表示用户ID
    // $arrTest[$b][1]  表示登陆起的时间
    // $arrTest[$b][1]  关联系统ID
     if  (ceil(($NowTime-$arrTest[$b][1])/60)>300){ //如果当前时间与一条记录的旧时间相差大于30分钟。
         if ($objsNickName==""){  //如果此用户session不存在,表示已经退出。
         //删掉。
          $temGlid= $arrTest[$b][2];   //关联系统ID
          $temuserid= $arrTest[$b][0]; //用户ID

网管bitscn_com


          $stmt = OCIParse($conn, "delete from tb_onlineuser where $intOnlineID='$temGlid' and N_ONLINEUSERID='$temuserid'");
          print  "delete from tb_onlineuser where $intOnlineID='$temGlid' and N_ONLINEUSERID='$temuserid'";
          OCIExecute($stmt);
          print "删除成功";
         //添加到统计表中
          $tmpTime=time(); //结束时间
          $DatLoginDate = date( "Y-m-d");//结束日期
          $DatLoginDate = "to_date('".$DatLoginDate."','YY/MM/DD')";
          $stmtUserCount = OCIParse($conn, "update tb_onlineusercount set D_OverDate=$DatLoginDate ,D_OverTime=$tmpTime where N_OnlineID='$temGlid'");//条件是相关联的系统ID

网管有家www.bitscn.net


          OCIExecute($stmtUserCount);
          print "添加成功到统计表中。";
         }else{
          $tmpTime=time(); //取得临时用户时间
          $temuserid= $arrTest[$b][0];
          $stmt = OCIParse($conn, "update tb_onlineuser set d_logintime=$tmpTime where N_ONLINEUSERID='$temuserid'");
          OCIExecute($stmt);
          print "更新成功";
          print $tmpTime;
        }
        }else{
网管u家www.bitscn.net

         print session_id();
         print "系统时间:".$NowTime."<br>";
         print "数据库中旧时间:".$arrTest[$b][1]."<br>";
         print "用户ID:".$arrTest[$b][0]."<br>";
         print "相差时间:".ceil(($NowTime-$arrTest[$b][1])/60)."<br>";
     }  
   }
}

/*如果要欢察统计表与在线表用户时间(当用户未离线时)
  select a.D_Logintime,b.D_logintime from tb_onlineuser a,tb_onlineusercount b
  where a.N_OnlineID=b.N_ONLINEID; 相差
   如果要统计出指定用户在线时间(当用户离线时)
  select D_logintime,D_OverTime from tb_onlineusercount where N_OnlineUserId='$USERID'; 相差
网管网www.bitscn.com

*/

?>

                      谢谢你的帮助!:_)

TAGs   方法   用户   在线   判断       "   //   时间   ID   系统   print      
 上一篇:我对权限控制系统的看法   下一篇:星期的计算函数
判断在线用户的方法 评论:
loading.. 评论加载中…
评论:请自觉遵守互联网相关政策法规,评论不得超过250字。

验证码: 注册用户
本类热门排行:
最新推荐文章:
网管论坛交流: