简体   繁体   中英

can I store session information in websocket server

I have a chat application using PHP web socket. and I have almost completed it.now when refresh the page the connection has broken from server and I have to reconnect to it.and my requirement is how to maintain the session for each user and .another problem is that when I try to connect to server from another tab of same browser then another connection is created with different port number. if I maintain session I can overcome from it please help me how to maintain session in PHP Web socket.

you can send session id every time you send a new message to websocket server and you can easily check session with session id. like this

session_id("your session_id");    
session_start();
if(isset($_SESSION["email/username/id whatever"]) && ($_SESSION["password"])){
keep connected and process request
}
else{
disconnect
}

You just need to start a session in the php page. You can use

session_start();

and then just pass the necessary variable to $_SESSION[] (it is the session array), then you can access them till you open the browser or you manually unset the session variable array or destroy the session.

To unset the session you can use

unset($_SESSION['variableName']);

or if you wish to destroy all session then you can use

session_destroy();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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