php只需检查在线或离线用户状态
检查用户在线或离线的最佳方法是什么? 在PHP中,有两个函数:php只需检查在线或离线用户状态,php,status,Php,Status,检查用户在线或离线的最佳方法是什么? 在PHP中,有两个函数: time(); 及 如何使用它们检查状态?简单,要检查用户状态,请执行以下操作: 1-在网站的每个文件(如数据库文件或会话文件)中,使用time()函数进行查询以更新用户的最后活动 if(isset($_SESSION['id'])){ $uid=$_SESSION['id']; $now=time(); $update=$db->prepare("update users set last_act=
time();
及
如何使用它们检查状态?简单,要检查用户状态,请执行以下操作: 1-在网站的每个文件(如数据库文件或会话文件)中,使用
time()
函数进行查询以更新用户的最后活动
if(isset($_SESSION['id'])){
$uid=$_SESSION['id'];
$now=time();
$update=$db->prepare("update users set last_act=$now where uid='$uid'");
$update->execute();
}
在profile page(配置文件页面)或任何要显示用户状态的页面中,获取用户的last_activity
记录和time()
,然后减去它们,如果差值超过300(以秒为单位),则用户处于脱机状态,否则用户处于联机状态
如果不使用占位符,准备好的语句是毫无价值的。您容易受到SQL注入的攻击。
var_dump(is_int('2')代码>输出bool(false)
,是一种类型测试$不会设置用户ID。你试过你的代码了吗?到目前为止,这不是一个合适的问题。也许你可以将你的非工作答案编辑成问题,并就你的方法哪里出了问题寻求指导。
if(isset($_SESSION['id'])){
$uid=$_SESSION['id'];
$now=time();
$update=$db->prepare("update users set last_act=$now where uid='$uid'");
$update->execute();
}
if (isset($_GET['pid'])) {
$now = time();
if (is_int($_GET['pid'])) {
$userid = $_GET['pid'];
}
$get_last_act = $db->prepare("select last_act from users where uid='$userid' ");
$get_last_act->execute();
$fetch = $get_last_act->fetch();
$last_act = $fetch['last_act'];
$difference = $now - $last_act;
if ($difference > 300) {
echo 'offline';
}
else {
echo 'online';
}
}