在Apache和WebSocket服务器之间使用PHP会话 [英] Using PHP session between apache and websocket server
问题描述
我正在使用来自 https://github.com/Flynsarmy/PHPWebSocket-Chat
我已经拥有Mysql的自定义用户帐户,该帐户的用户通过 login.php 登录到websocket。因此,我为特定用户设置了会话/ cookie。例如
I already have my custom users account with Mysql where user logs in at login.php connected to websocket. Thus I set session/cookie for the particular user. For an example
$_SESSION["user"] = 'xyz';//User is created.
问题是当我尝试使用 $ _ SESSION 时,即使 session_id()。我无法获取 $ _ SESSION 值。我是在 server.php 中执行此操作的,该服务器应该与 php server.php 命令一起运行。我尝试下面的代码。
The issue is when I try to use $_SESSION even with session_id(). I failed to get $_SESSION values. I am doing this in server.php which is suppose to be running with php server.php command. I try code below.
session_id("lsjdfjl');
@session_start();
$user = $_SESSION["user"];
我收到错误消息,指出未定义索引...
And I get error message saying undefine index ...
是否有其他方法可以将上述Websocket服务器与自定义用户数据库结合起来,并为不同的登录用户创建单独的聊天室,例如FB chat / gmail聊天系统。
Is there any alternative way to combine above Websocket server with custom user datab ase and create seperate chatroom for different logged in users. For example FB chat/gmail chat system.
推荐答案
您不能使用同一会话。
您根本无法将会话用于websocket连接。
You can't use session for websocket connection at all.
Websocket连接是一个单独的连接,位于不同的端口上,因此您的主会话在那里不可用
Websocket connection is a separate connection, on different port, so your main session is not available there.
您需要阅读有关基于令牌的Websocket身份验证的信息。以下是一些从 https://auth0.com/blog/2014/01/15/auth-with-socket-io/
You need to read about token-based websocket auth. Here is some info to start from https://auth0.com/blog/2014/01/15/auth-with-socket-io/
这篇关于在Apache和WebSocket服务器之间使用PHP会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!