简体   繁体   English

检查会话,使PHP连接保持活动状态

[英]Checking session, keeping PHP connection alive

We have a streaming site which has a loggin system based on PHP sessions. 我们有一个流媒体站点,该站点具有基于PHP会话的loggin系统。 After the user starts the stream, if another person is connecting with the same account, I need to terminate the first session and kill the page. 用户启动流之后,如果另一个人使用相同的帐户进行连接,则我需要终止第一个会话并杀死该页面。 The only way I can think of is using Ajax, but I'm not sure how secure is that. 我能想到的唯一方法是使用Ajax,但我不确定这样做的安全性。 Is there any way to output the page in php and after the page is served to the user and after that the script to keep checking if another user has logged in with the same account and kill the page? 有什么方法可以在php中输出页面,并在将页面提供给用户之后,然后在脚本中继续检查是否另一个用户已使用同一帐户登录并杀死该页面吗? Tried using output buffering and sleep, no luck so far. 尝试使用输出缓冲和睡眠,到目前为止还没有运气。

First of all, you need to keep track of sessions in a database or other storage system. 首先,您需要跟踪数据库或其他存储系统中的会话。 This data should have a reference to the user. 该数据应向用户提供参考。 And get cleaned up on a regular basis. 并定期清理。

If you kill an existing session, the user in that session wont be pushed out until the next server request. 如果您终止了一个现有会话,则该会话中的用户将不会被推出,直到下一个服务器请求。 So in the background, you want to 'ping' the server, just make an ajax request, until it fails. 因此,在后台,您要“ ping”服务器,仅发出ajax请求,直到失败为止。 On failure, redirect the user back to a login page. 失败时,将用户重定向回登录页面。

Now this is not foolproof. 现在这不是万无一失的。 A client could interfere with, block or redirect the ping requests. 客户端可能会干扰,阻止或重定向ping请求。 But if they were to access another page in your system, they would be forced to login again. 但是,如果他们要访问系统中的另一个页面,则将被迫再次登录。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM