How to force multiple sessions?

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
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

How to force multiple sessions?

Post by JeffG »

I seem to have a fundamental misconception of how sessions work, although I have been coding with them apparently successfully until I found out something today.

At the start of each php script, I have the following:

Code: Select all

 
session_cache_limiter("nocache");
session_start();
 
$self    = $_SERVER['PHP_SELF']."?".SID;
 
Today, I wanted to test something on my local server, logging in as two different users. So I logged in with one user, opened a new tab (this is Firefox) and logged in as the other user. I expected to get two different sessions, but the second login used, and overwrote, the existing session file. Can I force separate sessions, or as I said at the start, am I labouring under a fundamental misconception?

(Incidentally, I got exactly the same thing when I ran two separate instances of Firefox.)
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How to force multiple sessions?

Post by jackpf »

Firefox uses the same cookies for all windows and tabs. There's no way to have two of the same cookie.
Eric!
DevNet Resident
Posts: 1146
Joined: Sun Jun 14, 2009 3:13 pm

Re: How to force multiple sessions?

Post by Eric! »

You could use different session variables to "simulate" more sessions on the same browser if you need to. If the session exists, you could add a variable to expand it to a new session. I don't know why you would want to do that though....
JeffG
Forum Commoner
Posts: 35
Joined: Wed Jan 30, 2008 1:42 pm
Location: Newbury, UK

Re: How to force multiple sessions?

Post by JeffG »

OK, that explains something, thanks. It's a function of the browser, not the system it's running on. If I run one user in Firefox and the other in Internet Explorer, it works as I want it, but not both in the same browser. So that's all I need for local testing purposes.

I was a bit puzzled, because in the production system, several different computers are connecting to the server via an intranet, and it all works there.
User avatar
jackpf
DevNet Resident
Posts: 2119
Joined: Sun Feb 15, 2009 7:22 pm
Location: Ipswich, UK

Re: How to force multiple sessions?

Post by jackpf »

That's because each computer will save their own cookies, unless they're configured to use the same cookie file/database.
Post Reply