Page 1 of 1

Store Mutiple Sessions Simultaneously?

Posted: Tue Aug 21, 2007 1:09 pm
by hobo
Hi All,

I've run into an issue with sessions.

I create a session from a GET request like this:

$_SESSION['categoryID'] = $_GET['categoryID'];

What happens is that, when I open multiple browser windows/tabs from different categoryID's, the session variable gets overwritten with the newest value.

Sessions allow me to validate stuff at the beginning and all is good later.

I understand the concept of 1 session = 1 browser window/tab.

If I open a category where the categoryID=1 and then open multiple windows or tabs where categoryID=2 and categoryID=3 etc then, the $_SESSION variable is = 3.

Is there any possible way to tie the session to the newly opened browser window or tab. In this case, even if the user were to open new categories/windows, the older browser windows would still retain the session data.

I am looking for ideas on how to handle this issue when multiple windows are opened.


TIA

Posted: Tue Aug 21, 2007 1:20 pm
by feyd
Sessions are associated with the browser instances, typically. So no, new windows will not associate with new sessions. I would suggest redesigning your system to not need a session variable for these pages.

Posted: Tue Aug 21, 2007 1:48 pm
by hobo
Hello,

A re-design of the system is not an option. I do not want to throw GET vars only to have them validated every step.

TIA

Posted: Tue Aug 21, 2007 1:50 pm
by Christopher
You could do something like this:

Code: Select all

$browserID = md5($_SERVER['HTTP_USER_AGENT']);
$_SESSION[$browserID]['categoryID'] = $_GET['categoryID'];
I would certainly think long and hard about the implications of doing that though. I am not sure this is a real problem. And, saying that a "redesign is not an option" is saying that good design is not an option.

Posted: Tue Aug 21, 2007 2:59 pm
by superdezign
A 'redesign' wouldn't be difficult. All you'd need to do is pass the get variable to all of the links, and use it. I don't see the difficulty in implementing that.

Posted: Tue Aug 21, 2007 4:37 pm
by pickle
I understand your hesitance to add a GET variable to all your links. However, I'm not sure there's a choice. Yes you can create a random string for each session & make your $_SESSION variable a multi-dimensional array, but there's no way to pass that information between page loads without passing them via $_GET or $_POST. Normally of course you could use a session or cookie, but obviously that won't work.

Unfortunately I can't see any way other than passing a GET variable.

Posted: Wed Aug 22, 2007 12:28 am
by hobo
pickle wrote:Yes you can create a random string for each session & make your $_SESSION variable a multi-dimensional array, but there's no way to pass that information between page loads without passing them via $_GET or $_POST.
I'm curious to know how a $_SESSION multi-dimensional array would work. Are you saying I could pass the session_id through a GET statement? Could you please post an example on how to implement this..

TIA

Posted: Wed Aug 22, 2007 9:46 am
by pickle
Pretty much just like what ~arborint posted, but make the $browserID a random, unique string.

Posted: Wed Aug 22, 2007 11:40 am
by hobo
arborint wrote:You could do something like this:

Code: Select all

$browserID = md5($_SERVER['HTTP_USER_AGENT']);
$_SESSION[$browserID]['categoryID'] = $_GET['categoryID'];
I think I've missed something.. How would the HTTP_USER_AGENT differentiate a session when multiple tabs of different category IDs are opened?

These are the type of links that the user would open multiple browser windows/tabs.
.com/config.php?categoryID=1
.com/config.php?categoryID=2
.com/config.php?categoryID=3
.
.
.com/config.php?categoryID=20

The config.php has this bit of code:
$_SESSION['categoryID'] = $_GET['categoryID'];
Each opened tab needs to maintain the session or the categoryID.

TIA

Posted: Wed Aug 22, 2007 11:44 am
by pickle
pickle wrote:...make the $browserID a random, unique string.

Posted: Wed Aug 22, 2007 12:03 pm
by Christopher
Sorry, I thought you wanted it for different browsers. I think I have the perfect to your unknowingly insane request! ;)

Code: Select all

//$_SESSION['categoryID'] = $_GET['categoryID'];