直接上源码。
在对应登录状态查询的php文件中加入如下代码:
$username=$_REQUEST['username'];
//必须开启session才能获取session_id 否则将为空
session_start();
$session_id = session_id();
//查看用户的session_id 如果为空则重新生成
$sql="update ".$ecs->table('users')." set session_id='{$session_id}' where
user_name='{$username}'";
$db->query($sql);
//将session_id和用户名存储到session中
$_SESSION['s_id']=$session_id;
$_SESSION['u_name']=$username;
本网站引用的公共文件,加入下面的session验证代码(验证session_id是否过期,或是否一致。)
$u_name=$_SESSION['u_name'];
if($u_name){
//查看session_id,登录时间
$sql_id="select session_id,last_login from ".$ecs->table('users')." where
user_name='{$u_name}'";
$ss=$db->getRow($sql_id);
$time=time();
//如果登录时间超过自己指定的时间可以强制下线重新登录
if(($time-$ss['last_login']) > 1800){
//修改session_id
$sql="update ".$ecs->table('users')." set session_id='' where
user_name='{$u_name}'";
$db->query($sql);
echo "<script> window.location.href='http://localhost/user.php?act=logout';
alert('您已经登陆超时请重新登陆');</script>";
}
}
//如果数据库session_id和当前获取的session_id不一致提示已经登录
if($ss['session_id'] != $_SESSION['s_id']){
echo "<script> window.location.href='http://localhost/user.php?act=logout';
alert('您已经在其他地区登陆');</script>";
}