Store Mutiple Sessions Simultaneously?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
hobo
Forum Newbie
Posts: 4
Joined: Tue Aug 21, 2007 1:06 pm

Store Mutiple Sessions Simultaneously?

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
hobo
Forum Newbie
Posts: 4
Joined: Tue Aug 21, 2007 1:06 pm

Post 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
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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.
(#10850)
User avatar
superdezign
DevNet Master
Posts: 4135
Joined: Sat Jan 20, 2007 11:06 pm

Post 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.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post 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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
hobo
Forum Newbie
Posts: 4
Joined: Tue Aug 21, 2007 1:06 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Pretty much just like what ~arborint posted, but make the $browserID a random, unique string.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
hobo
Forum Newbie
Posts: 4
Joined: Tue Aug 21, 2007 1:06 pm

Post 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
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

pickle wrote:...make the $browserID a random, unique string.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
User avatar
Christopher
Site Administrator
Posts: 13596
Joined: Wed Aug 25, 2004 7:54 pm
Location: New York, NY, US

Post 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'];
(#10850)
Post Reply