我在php / ajax中建立聊天。 为了检测用户超时,我编写了一个后台PHP脚本,如果5秒内没有活动,则断开用户连接。
问题是,会话中的变量没有更新......
这是我的chat.php,其中执行了所有ajax查询(每个客户端至少每秒1次):
<?php session_save_path("sessions"); session_start(); $_SESSION['last_request'] = time();以下是我从这个chat.php启动后台脚本的方法:
exec('php user_manager.php ' . session_id() . ' > /tmp/output &');这是我的循环来检查用户是否仍在这里(这是后台进程):
<?php session_save_path("sessions"); session_id($argv[1]); session_start(); ignore_user_abort(true); set_time_limit(0); echo "background task starting on user " . $_SESSION['user'] . PHP_EOL; $lastrequest = $_SESSION["last_request"]; while(time() - intval($lastrequest) < 5) { //5 seconds echo "last_request : " . $lastrequest . PHP_EOL; $lastrequest = $_SESSION["last_request"]; sleep(1); } echo "disconnecting user !" . PHP_EOL;正如您所看到的,last_request会话变量永远不会更新:
# cat /tmp/output background task starting on user test last_request : 1493120020 last_request : 1493120020 last_request : 1493120020 last_request : 1493120020 last_request : 1493120020 disconnecting user !我不明白为什么,它让我发疯...请帮忙!
I'm building a chat in php/ajax. In order to detect users timeout, I wrote a background php script which disconnects the user if there is no activity for 5 seconds.
The problem is, the variable in the sessions are not updated...
Here is my chat.php where all the ajax queries are performed (at least 1 per second and per client) :
<?php session_save_path("sessions"); session_start(); $_SESSION['last_request'] = time();Here is how I launch the background script from this chat.php :
exec('php user_manager.php ' . session_id() . ' > /tmp/output &');And here is my loop to check if the user is still here (this is the background process) :
<?php session_save_path("sessions"); session_id($argv[1]); session_start(); ignore_user_abort(true); set_time_limit(0); echo "background task starting on user " . $_SESSION['user'] . PHP_EOL; $lastrequest = $_SESSION["last_request"]; while(time() - intval($lastrequest) < 5) { //5 seconds echo "last_request : " . $lastrequest . PHP_EOL; $lastrequest = $_SESSION["last_request"]; sleep(1); } echo "disconnecting user !" . PHP_EOL;And as you can see, the last_request session variable is never updated :
# cat /tmp/output background task starting on user test last_request : 1493120020 last_request : 1493120020 last_request : 1493120020 last_request : 1493120020 last_request : 1493120020 disconnecting user !I don't understand why and it's driving me crazy... Please help !
最满意答案
$ _SESSION数组在session_start处启动,但在用户聊天脚本不会修改之后。 您应该在每个会话中使用一个文件,其中chat.php写入上次访问时间。 后台进程必须在while块中读取此文件。
聊天脚本:
<?php session_save_path("sessions"); session_start(); file_put_contents('access_' . session_id() . '.txt', time());后台流程:
<?php session_save_path("sessions"); session_id($argv[1]); session_start(); ignore_user_abort(true); set_time_limit(0); echo "background task starting on user " . $_SESSION['user'] . PHP_EOL; $lastrequest = file_get_contents('access_' . session_id() . '.txt'); while(time() - intval($lastrequest) < 5) { //5 seconds echo "last_request : " . $lastrequest . PHP_EOL; $lastrequest = file_get_contents('access_' . session_id() . '.txt'); sleep(1); } echo "disconnecting user !" . PHP_EOL;The $_SESSION array is initiated at the session_start, but after it won't be modified by the user chat script. You should use a file per session where the chat.php write the last access time. The background process have to read this file in the while block.
Chat script:
<?php session_save_path("sessions"); session_start(); file_put_contents('access_' . session_id() . '.txt', time());Background process:
<?php session_save_path("sessions"); session_id($argv[1]); session_start(); ignore_user_abort(true); set_time_limit(0); echo "background task starting on user " . $_SESSION['user'] . PHP_EOL; $lastrequest = file_get_contents('access_' . session_id() . '.txt'); while(time() - intval($lastrequest) < 5) { //5 seconds echo "last_request : " . $lastrequest . PHP_EOL; $lastrequest = file_get_contents('access_' . session_id() . '.txt'); sleep(1); } echo "disconnecting user !" . PHP_EOL;更多推荐
发布评论