Can I name a session variable using another variable?
| From: | John Gostick | Date: | Fri, 10 Sep 2004 21:47:17 +0000 |
| Subject: | Can I name a session variable using another variable? | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-196367@lists.php.net to get a copy of this message | ||
Hi,
I've encountered a problem I can't seem to find much reference to on the web, so I was
wondering if anyone here could help me...
Fisrt a brief background:
I am building a fairly simple PHP/MySQL chat system with multiple rooms. Each room is loaded from
the main chat page by clicking on a link that uses JavaScript to open and size a new window, and
also passes the roomname as a variable to the new chat window. Messages sent from a room are tagged
with the roomname, and the window refreshes periodically to look for new messages(rows) in the
database that have that rooms name. In this way a room only recieves messages meant for it. So far
so good. However, to determine what messages are new, a SESSION variable $prevtime is set each time
the script checks for new messages, so that next time it checks, it will only download messages
posted SINCE the time given in $prevtime. This was not a problem with a single room, but when
multiple rooms are open they are sharing the same session variable $prevtime because they are all
using the same session as they were all opened from the same window. This leads to
'skipping' of messages and not all messages being picked up by each room.
I've stratched my head over this, and decided to try and generate a unique SESSION variable for
each room, named using the string 'prevtime' prefixed with the value of the variable
$roomname (the name of the current room). However I can't seem to get this to work.
To summarise my problem: Can I name/create a session variable like this?:
$roomname="sampleroom";
$_SESSION["$roomname" . "prevtime"]=time();
With the aim of making a session variable named 'sampleroomprevtime'. It is possible to
use a variable in naming a session this way? I would really appreciate any help anyone could give me
in making this work, or any suggestions of a better way from people with a much better knowledge of
sessions than myself!
Thanks in advance,
John