使用会话 ID 检查会话状态 [英] checking status of session with session id
问题描述
我正在尝试开发一个聊天应用程序,我需要知道其他用户是否处于活动状态.我已经完成了时间戳"示例(如果最后一次活动超过指定时间,则将用户视为离线)和这种方法的问题是这段代码需要通过javascript定期重复执行.
I am trying to develop a chat application and I need to know whether the other user is active or not.I already went through "timestamp" example( if the last activity is more than specified time consider the user as offline ) and the problem with this method is that this code need to executed repeatedly in regular intervals through javascript.
所以,我在想,如果有任何方法可以通过知道会话 ID 来检查服务器中的会话是否处于活动状态.如果可能的话,我会在 MYSQL 表中为上次会话 ID"创建一个额外的列我会在每次发送消息时检查它是否处于活动状态或关闭状态,我可以确定对方是否处于活动状态.
So, I was thinking that if there is any way to check whether a sessions is active or not in server by knowing the session ID.If this is possible I am making an extra column for "last session id" in MYSQL table and I will check if it is active or closed the every time I send a message and I can know for sure if the person on the other side is active or not.
推荐答案
您可以使用 ajax 和 php:
You can make this with ajax and php:
所以你必须在javascript中设置setInterval:
So you have to set in javascript the setInterval:
setInterval(function() {
$.ajax({
url: 'ajax.php',
params:{
action:'checkSessionTime'
},
success: function(response) {
var resp = JSON.parse(response.responseText);
if(resp.state != 'valid')
{
location.reload();
}
}
});
}, 1000 * 60* 10);
在 PHP 中,你有一个这样的函数:
In PHP you have a function like this:
function checkSessionTime(){
if($_SESSION['YourSessionId oder YourUserId']){
echo json_encode(array('success' => true, 'state' => 'valid'));
}else{
echo json_encode(array('success' => true, 'state' => 'expired'));
}
}
这篇关于使用会话 ID 检查会话状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!