简体   繁体   English

PHP SESSION在websocket服务器中不起作用

[英]PHP SESSION not working in websocket server

I have a simple PHP websocket for real time chat application. 我有一个用于实时聊天应用程序的简单PHP Websocket。 I use apache server as well as websocket server. 我使用apache服务器以及websocket服务器。

When username logs in successfully, I set SESSION with value username as below in login.php 当用户名成功登录后,我在login.php中使用值username设置SESSION。

@session_start();
$_SESSION["user"] = $username;

I have to start websocket server by php server.php . 我必须通过php server.php启动websocket服务器。

Hence I cannot get value of $_SESSION["user"] even I start session. 因此,即使我开始会话,我也无法获得$ _SESSION [“ user”]的值。 I gives an error as Undefined index user . 我作为未定义索引用户给出了错误。

Someone on the PHP documentation are crazy enough to attempt this (and my answer is based from that person) PHP文档上的某人疯狂地尝试了此操作(我的回答是基于该人的)

But basically for you to switch between your current web based to command line you need to grab the current session id from the web and set it back on the cli. 但基本上,您要在当前的基于Web的命令行之间切换,就需要从Web上获取当前的会话ID,并将其重新设置为cli。

Write_session.php Write_session.php

<?php

session_start();
$_SESSION['test']='test';

print_r($_SESSION);

echo session_id();

?>

Output of write_session.php write_session.php的输出

gqptpc3scf4k0h3vpl2d341u13

Read_session.php (you need to find a safe way to send the session id over I suppose) Read_session.php(您需要找到一种安全的方式来发送会话ID,我想是这样)

<?php

$ssid = "gqptpc3scf4k0h3vpl2d341u13";

session_id ($ssid);
session_start();

print_r($_SESSION);

?>

Reference : http://php.net/manual/en/function.session-start.php#53293 参考: http : //php.net/manual/zh/function.session-start.php#53293

It is possible, however to start a session with the Apache server and then access the session cookie within the websocket code. 但是,可以启动与Apache服务器的会话,然后在websocket代码中访问会话cookie。

Apache server's file starts the session. Apache服务器的文件开始会话。 You can send session id every time you send a new message to websocket server and you can easily check session with session id. 每次将新消息发送到websocket服务器时,您都可以发送会话ID,并且可以轻松地使用会话ID检查会话。

Look this: 看看这个:

session_id("your session_id");    
session_start();
if(isset($_SESSION["SESSION_NAME"])){
     //keep connected and process request
}
else{
     //disconnect
}

OR 要么

You can check step by step example from here . 您可以从此处逐步查看示例。

The session ID is available in a cookie, usually named PHPSESSID (depends on the session.name php.ini setting, and can be checked and changed during execution with session_name() . 会话ID在cookie中可用,通常称为PHPSESSID (取决于session.name php.ini设置),并且可以在执行期间使用session_name()进行检查和更改。

The HTTP headers, including cookies, are available to the WebSocket server when the user is connecting. 用户连接时,HTTP头(包括cookie)可用于WebSocket服务器。 A responsible WS server should store those headers and make them available to the application through the user object (or connection object, or whatever object stores and uniquely identifies the resource through which to send the message back to the connected client). 负责任的WS服务器存储这些标头,并通过用户对象(或连接对象,或任何存储的对象)将其提供给应用程序,并唯一标识用于将消息发送回连接的客户端的资源。

Also, note that the PHP session system locks a user's session while it is open. 另外,请注意,PHP会话系统在打开时会锁定用户的会话。 Because WebSocket servers are designed to run continuously, they will never unlock the session on their own. 因为WebSocket服务器被设计为可以连续运行,所以它们永远不会自行解锁会话。

So, if a user's session is opened in WS, then every single AJAX and normal web request will timeout. 因此,如果在WS中打开用户会话,则每个单个AJAX和正常的Web请求都将超时。 That user will not be able to use your site at all. 该用户将完全无法使用您的网站。

Also, only one session can be open in a script at a time. 同样,一次只能在一个脚本中打开一个会话。 If a second user connects to the WS server, then that user will have access to all of the session information of the first user, and no access to their own session. 如果第二个用户连接到WS服务器,则该用户将有权访问第一个用户的所有会话信息,而无权访问他们自己的会话。 (Kinda a big, giant, gaping security hole.) (Kinda是一个巨大的巨大安全漏洞。)

Thus, if you open a session, you MUST close the session as soon as possible using session_write_close() . 因此,如果您打开会话,则必须使用session_write_close()尽快关闭该会话。

See this previous question and answer for a specific implementation of sessions in a WebSocket server, as an example of what to look for in your implementation. 有关在WebSocket服务器中会话的特定实现的信息,请参见前面的问题和答案,以作为在实现中寻找内容的示例。 (Note: Some may be tempted to call this question a duplicate. While I'd hope that both questions stay active, I think, of any, that this question here should be the one to stand, because it is more general (does not cover one specific WS server), and the advice given in the last question is going to be obsolete soon, due to active development with that WS server.) (注意:有些人可能会倾向于将此问题重复。尽管我希望这两个问题都保持活跃,但我认为在任何情况下,该问题都应该放在这个问题上,因为它比较笼统(不涵盖一台特定的WS服务器),由于该WS服务器的积极开发,最后一个问题中的建议很快就会过时。)

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

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