Forum Moderators: coopster

Message Too Old, No Replies

Store Mutiple Sessions Simultaneously?

         

HoboTraveler

11:48 am on Aug 12, 2007 (gmt 0)

10+ Year Member



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 from different categoryID's, the session variable gets overwritten with the newest value.

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 have the session variable remain the same and not update itself everytime a new window is opened?

How do I modify this?

TIA

Habtom

11:55 am on Aug 12, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you don't want it assigned back again, once it is assigned, you might want to do the following:

if!isset($_SESSION['categoryID']){
$_SESSION['categoryID'] = $_GET['categoryID'];
}

Habtom

HoboTraveler

7:00 pm on Aug 12, 2007 (gmt 0)

10+ Year Member



Hello,

Thanks, but that does not work.

What happens is, the user opens multiple browser windows or tabs. Hence, categoryID=1,categoryID=2, categoryID=3 etc. Each of these needs to be saved as a unique session.

The session variable would need to be overwritten and each of the newly opened categoryID instance needs to be saved as a session. The previously opened browser windows need to preserve the same categoryID though.

Any ideas?

TIA

jatar_k

7:56 pm on Aug 12, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



you can only really maintain one session per user per browser

if they used more than one browser then you could maintain more than one session

as it stands the browser/session behaviour you are seeing is the appropriate behaviour

roosevelt

9:00 pm on Aug 12, 2007 (gmt 0)

10+ Year Member



If i were you I would do this.

Instead of storing sessions as a flat value, I would use array. So something like:

if(isset($_SESSION['categoryID']))
{
$_SESSION['categoryID'] = $_GET['categoryID'];
}
else
{
$_SESSION['categoryID'] = array($_GET['categoryID']);
}

So if you need to access the categories visited you can do this:

$_SESSION['categoryID'][0], $_SESSION['categoryID'][1], where 0 and 1 is the index :).

HoboTraveler

5:21 am on Aug 13, 2007 (gmt 0)

10+ Year Member



@jatar_k,

Yes, could you please point me in the direction on how to maintain the one user, multiple browser feature.

To recap, these are the list of links that the user would open multiple browser window links with. Note that, the GET request is just a categoryID, nothing else to it.

.com/config.php?categoryID=1
.com/config.php?categoryID=2
.com/config.php?categoryID=3
.
.
.com/config.php?categoryID=20

[edited by: HoboTraveler at 5:22 am (utc) on Aug. 13, 2007]

Habtom

5:38 am on Aug 13, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Alternatively, you can store the value in hidden text box, and repost it when a form is submitted or browser is reloaded.

If you want to stick to the SESSION solution, every session name need to be unique, and just the way it is I don't see a quick solution.

Or try the SESSION array solution given above.

jatar_k

12:54 pm on Aug 13, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



if you open IE and firefox, for example, it hould work already,they won't corrupt each other's sessions. If you have multiple instances/windows of the same browser open then they will overwrite the session

this is really a logic problem more than anything else.

What functionality are you trying to acheive?

does it have to do with tracking?

HoboTraveler

10:30 am on Aug 15, 2007 (gmt 0)

10+ Year Member



@jatar_k,

I generate a list of twenty categories.

The URLS are like these:
.com/config.php?categoryID=1
.com/config.php?categoryID=2
.com/config.php?categoryID=3
.
.
.com/config.php?categoryID=20

Each category has unique forms.

When the user clicks on a category, the category ID is stored in a session variable. Session variables are a must because the ID's are reused across multiple forms in the same category.

The session variables begin to create an issue when multiple tabs/browser windows are opened.

My goal is to allow users to open multiple tabs and allow them to work on multiple categories simultaneously.This is why I need the session variables to store the category ID's uniquely and not have them overwritten.

Ideas welcome..

Habtom

11:13 am on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



the category ID is stored in a session variable

Why do you want to store it in SESSION variable? You have the categoryID posted on the URL. If you wanted to use it on the new page, you can just use it from the $_POST or $_REQUEST array.

What is behind storing it in a SESSION variable?

jatar_k

2:38 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



>> My goal is to allow users to open multiple tabs and allow them to work on multiple categories simultaneously.

well you are going against the session logic and how browsers hadnle it, you need to change you logic, as Habtom mentioned, just maintain it in the url or find a way to store multiple id's in the session and be able to figure out which to use where

HoboTraveler

5:34 am on Aug 20, 2007 (gmt 0)

10+ Year Member




What is behind storing it in a SESSION variable?

Well, each category has atleast 20 forms. Each form has to go through multiple submit levels. Its not easy as just a POST.

I also need to pull data out associated with the ID and store the vars in session vars.

Also, initially, I validate the GET variable ID and if true store it in a session var. This prevents me from resending all those GET vars in every request. And if I did use GET vars, I'd have to validate them at every step and initialize a lot of variables.

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

This works great. But not so great when multiple windows/tabs are opened.

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

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

One option is not to overwrite the session vars if they have been initialized already. However, the user does need to select a new category by going back home. So a category session ID would need overwritten anyway.

I guess this leaves us with one option. 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.

How feasible would this be? Is it possible to tie a session and make it unique maybe to the PID or browser tab?

Would this be possible with cookies? Can cookies be associated to individual browser windows/tabs?

Ideas welcome..

TIA

Habtom

6:06 am on Aug 20, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The reasons you have mentioned to use SESSIONs instead of the GET OR POST arrays, is perfectly reasonable.

I tried to get you a solution for that, it seems there won't be away around it. May be somebody else will give you a hint.

Well, each category has atleast 20 forms. Each form has to go through multiple submit levels. Its not easy as just a POST.

If it is that long, perhaps they should stick to one of the categories.

Habtom

borntobeweb

6:11 am on Aug 20, 2007 (gmt 0)

10+ Year Member



Let's say you call each separate tab a "thread". Your session contains an array of threads indexed by a threadID, so

$threadID = something;
$_SESSION[$threadID] = array();

As Habtom mentioned, you carry the threadID in the URL, or in a hidden form element across your steps (just like PHP does with sessionID when cookies are turned off), and when you want to save some data you save it in the appropriate array element, e.g.

$threadID = $_REQUEST['threadID'];
$_SESSION[$threadID]['value1'] = $_POST['value1'];
$_SESSION[$threadID]['value2'] = $_POST['value2'];
etc.

The threadID can be the same as your categoryID to keep it simple, or you can get more fancy and allow multiple tabs for the same category by setting the threadID to the current timestamp when the user starts a new category.

HoboTraveler

7:13 pm on Aug 20, 2007 (gmt 0)

10+ Year Member





$_SESSION['categoryID'] = array($_GET['categoryID']);

This does not seem to work. The array seems to contain just one variable and does not increment.

moroose

8:21 pm on Aug 20, 2007 (gmt 0)

10+ Year Member



A session has only one common session name-PHPSESSID.So if you want multiple session ids for one session (for multiple tabs),you need to devise "multiple session names".I mean virtual ones,as in virtual hosts

That a user leaves a form half filled and moves to fill another makes no sense to me . I would simply alert them with javascript to complete the curent form before cliking on another.
If each of your forms spans over multiple pages,the session will likely expire anyways by the time they are filling more than one simultlaneously(assuming the volatile 24 minutes expiry default)
yes,it can be done the way you want it somehow,but may be you should not.
You will have to modify every link,form action,and even javascript,img src,iframe,would you want that additional load?

There's a thread in [php.net...] about that.Hope it'll inspire you somehow.
Good luck!

HoboTraveler

10:38 am on Aug 23, 2007 (gmt 0)

10+ Year Member



Hi Guys,

Thank-You all for turning in your ideas..

I ended up implementing multi-dimensional arrays and am able to call session arrays based on the category called. Individual tabs or browser windows do not mangle the session vars now..

Works great!

Thanks

[edited by: HoboTraveler at 10:39 am (utc) on Aug. 23, 2007]