【发布时间】:2015-06-20 22:07:17
【问题描述】:
请告诉我如何通过访问其会话来显示用户在线或离线。 这是php和mysql查询
header('Content-Type: application/json');
$array = array();
$res = mysql_query("SELECT * FROM `posts` WHERE status=1");
if(mysql_num_rows($res) > 0){
while($row = mysql_fetch_assoc($res)){
$array[] = $row['user_id']; // this adds each online user id to the array
}
}
echo json_encode($array);
我试试这个代码这是js文件
<script type="text/javascript">
$(document).ready(function() {
setInterval(function(){
$.ajax({
url: 'status.php',
dataType: "json",
type: 'GET',
success: function(data) {
if (data.length > 0){ // if at least 1 is online
$('.status').each(function(){ // loop through each of the user posts
var userid = parseInt($(this).attr('id').replace('user','')); // get just the userid #
if($.inArray(userid, data) !== -1){ // if userid # in the returned data array set to online
$(this).css({background: 'green'});
} else{ // else if userid # not in the returned data array set to offline
$(this).css({background: 'grey'});
}
});
}
else { // if no one is online, set all to offline
$('.status').css({background: 'grey'});
}
}
});
}, 2000); //2s just for testing. Set to 15s when code fully works.
});
</script>
但是这段代码不是根据我的要求运行的,它是根据我每次保存在数据库中的状态在数据库中保存的,然后根据它的行为它给我结果。 我想要什么
- 当用户关闭其浏览器然后逻辑上它的会话被破坏然后我想显示它的离线状态
2.如果用户保持他的会话,那么它的状态是在线
【问题讨论】:
-
您在代码中哪里使用过该会话
标签: php jquery mysql ajax session