Why does multiple sessions started on the same page gets the same session id?

Posted by Calmarius on Stack Overflow See other posts from Stack Overflow or by Calmarius
Published on 2010-06-01T12:00:54Z Indexed on 2010/06/01 12:13 UTC
Read the original article Hit count: 245

Filed under:
|
|
|
|

I tryed the following:

<?php

session_name('user');
session_start();
// sets an 'user' cookie with session id.
// This session stores the user's login data.
// Its an ordinary session.
$USERSESSION=$_SESSION; // saving this session
$userSessId=session_id();
session_write_close(); //closing this session

session_name('chatroom');
session_start();
// set a 'chatroom' cookie but contains the same session id and points to the same data :( .
// This session would be used to store the chat text. This session would be
// shared between all users joined in this chat room by setting the same session id for all members.
// by calling session_regenerate_id would make a diffent session id for every user and it won't be a chat room anymore.

?>

So I want to do a chat like thing with sessions. On the client side it would be done with ajax that polls this php page in every 5-10 seconds. Sessions may be cached in the server's memory so it can be accessible fast. I can store the chat in the database but my service runs on a free webhost which is limited, only 4 mysql connections allowed at a time which is almost nothing. I try to touch my database as least times as possible.

© Stack Overflow or respective owner

Related posts about php

Related posts about AJAX