RE: [PHP] Can I name a session variable using another variable?

From: Date: Mon, 13 Sep 2004 17:27:49 +0000
Subject: RE: [PHP] Can I name a session variable using another variable?
Groups: php.general 
Request: Send a blank email to php-general+get-196532@lists.php.net to get a copy of this message
Ok, assuming everything's working correctly except for the use of $prevtime in the SESSION, then here's one answer: Yes, you can name a session the way you described. If you're having trouble with doing the concatenation of values inside $_SESSION, then try combining them outside: $roomname="sampleroom"; $sessionname = $roomname . $prevtime; $_SESSION[$sessionname]=time(); Secondly, looks like you forgot a "$" on prevtime below: $_SESSION["$roomname" . "prevtime"]=time(); Thirdly, you can store arrays in $_SESSION as well, so you could divide the rooms up that way: $_SESSION[$roomname]["PrevTime"] = $prevtime; -TG > -----Original Message----- > From: John Gostick [mailto:php@oldmanstone.net] > Sent: Friday, September 10, 2004 5:47 PM > To: PHP General > Subject: [PHP] Can I name a session variable using another variable? > > > 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 >

« previous php.general (#196532) next »