Home > php教程 > php手册 > body text

判断在线用户的方法

WBOY
Release: 2016-06-21 08:58:17
Original
732 people have browsed it

在线

解决了昨天的问题:表结果变了一下,如下:
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),       --登陆时间以秒计
     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)             ,    --结束时间
     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)){
     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");
            }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);     
       while(OCIFetchInto($stmOnlineFlag,&$arronlineFlag,OCI_NUM)){
              if ($arronlineFlag[0]!=0){                               //表示已经在线
                                                                      //先取到在线用户关联系统ID
                  $stmtSysID= OCIParse($conn,"select N_ONLINEID from tb_onlineuser where N_ONLINEUSERID='$intOnlineUserID'");
                  OCIExecute($stmtSysID);
                  while(OCIFetchInto($stmtSysID,&$arrSysID,OCI_NUM)){
                      $SysID=$arrSysID[0];          
                   }//while_Over                                     //找完后踢出该用户
                  $stmt = OCIParse($conn, "delete from tb_onlineuser where N_ONLINEUSERID='$intOnlineUserID'");
                  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
                  OCIExecute($stmtUserCount);
                  print "添加成功到统计表中。";
                }//endif                                              //不在线正常注册
       $objsNickName=$name; //注册Session变量
       unset($arrPwd);             //撤消临时数组
       srand((double)microtime()*1000000000);
       $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)");
       OCIExecute($stmtC);
       Header("Location:index.php");  //成功登陆!
          }//whileOVER
        }//end if
      }//while_Over
    }//end if
}//while_Over

?>

/*-------CheckSession-----检查刷新程序---*/

/*30分钟刷新程序
  先统计出在线的用户数,如果没有在线用户,系统要保证一个系统指定用户。该系统用户时时在线的原因是保证该刷新程序的执行
  如果该登陆用户Session不存在了,表示用该用户离线。统计出时间。
*/
  session_start();
  require('oracle8conn.php');
  print $objsNickName;
?>
  



$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."
";
//判断在线否?
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]);     
  }
}    
   for($b=0;$b    for($a=0;$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
          $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
          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{
         print session_id();
         print "系统时间:".$NowTime."
";
         print "数据库中旧时间:".$arrTest[$b][1]."
";
         print "用户ID:".$arrTest[$b][0]."
";
         print "相差时间:".ceil(($NowTime-$arrTest[$b][1])/60)."
";
     }  
   }
}

/*如果要欢察统计表与在线表用户时间(当用户未离线时)
  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'; 相差
*/

?>

                      谢谢你的帮助!:_)



Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!