0

I have a simple PHP websocket for real time chat application. I use apache server as well as websocket server.

When username logs in successfully, I set SESSION with value username as below in login.php

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

I have to start websocket server by php server.php.

Hence I cannot get value of $_SESSION["user"] even I start session. I gives an error as Undefined index user.

4
  • Do you have session_start(); in server.php? Commented Jul 21, 2015 at 6:36
  • stackoverflow.com/questions/23230830/… Commented Jul 21, 2015 at 6:38
  • Yes I did start session with session_start(); Commented Jul 21, 2015 at 6:39
  • Make sure that session_save_path() and session_name() are the same whether you're accessing it through the web or through CLI. Also, most important, do not forget to run session_write_close() often; especially in a WebSocket server. Commented Jul 21, 2015 at 13:00

3 Answers 3

0

Someone on the PHP documentation are crazy enough to attempt this (and my answer is based from that person)

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.

Write_session.php

<?php

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

print_r($_SESSION);

echo session_id();

?>

Output of write_session.php

gqptpc3scf4k0h3vpl2d341u13

Read_session.php (you need to find a safe way to send the session id over I suppose)

<?php

$ssid = "gqptpc3scf4k0h3vpl2d341u13";

session_id ($ssid);
session_start();

print_r($_SESSION);

?>

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

Sign up to request clarification or add additional context in comments.

1 Comment

I still cannot get SESSION in server.php. I set SESSION["username"] in login.php neither COOKIE. Its blank array. Any other solution?
0

It is possible, however to start a session with the Apache server and then access the session cookie within the websocket code.

Apache server's file starts the session. You can send session id every time you send a new message to websocket server and you can easily check session with session 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.

Comments

0

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().

The HTTP headers, including cookies, are available to the WebSocket server when the user is connecting. 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).

Also, note that the PHP session system locks a user's session while it is open. Because WebSocket servers are designed to run continuously, they will never unlock the session on their own.

So, if a user's session is opened in WS, then every single AJAX and normal web request will timeout. 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. (Kinda a big, giant, gaping security hole.)

Thus, if you open a session, you MUST close the session as soon as possible using 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. (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.)

4 Comments

Web sockets need more work. all of this seems really hacky maybe a dedicated web sockets server from apache would do it.
@Alex_Nabu: Apache's web server would be the wrong choice, because HTTP traffic and WebSocket traffic are such a different paradigm. A WebSocket server written in the same language as the application is really the best choice, especially since there is no way to put CGI-like interfaces into a WS connection (which is how web servers handle sending requests to PHP for web traffic). -- The warts and hacks all rest squarely on PHP's shoulders, but any language will have its own issues unless something like WS is explicitly designed into the language.
I'm just thinking more along the lines of standardization. One of the things that made web developing the largest dev community was that there was a defacto standard way of accomplishing everything. Websockets kinda changes that. TBH there isnt even any reason to use php at all now considering you could do a socket server in any language you want. What I'm getting at is there should be some popular implementation that kinda standardizes how we do things while dealing with web sockets. Some popular lib that makes things nice and simple like php did while working under apache/nginx ect
@Alex_Nabu: Right now, the big two are Ratchet and my own PHP-Websockets, similar to how the big two web servers on Linux are Apache HTTPd and nginx. (Google has a very simple WebSocket class example, as well, but that's gone the way of Tim Berners-Lee's web server.) There are also many other WebSocket servers for other platforms, such as Socket.io for Node.js applications, but <opinion type="personal" context="joke">they're as evil and cancerous as .NET and IIS</opinion>.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.