PHP Session Sharing

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
williamshen25
Forum Newbie
Posts: 10
Joined: Fri Sep 03, 2010 5:45 pm

PHP Session Sharing

Post by williamshen25 »

Hi all

I have a very basic question. what is the range on PHP session variables? if I open the same PHP page on the same computer in different browsers, they share the same session. Is there a way to make different browser share different sessions??
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Session Sharing

Post by Jonah Bron »

I may be wrong, but I'm pretty sure sessions are browser specific. Each browser keeps track of it's own cookies, and the session ID is usually stored in a cookie. Are you sure they shared the same session?
williamshen25
Forum Newbie
Posts: 10
Joined: Fri Sep 03, 2010 5:45 pm

Re: PHP Session Sharing

Post by williamshen25 »

that s something I used to believe... but I tested it in two browsers going to the same page. if I change session variable in one page and refresh the other, the other changes the output.

I found if I close all the IE window the session will be killed, but lets say I have window A, B, C open and A and B pointing to my test page while C point to some random page. If I just close A and B and open up window D to go to my test page, my session still exist.

Can this be some setting in PHP.ini?? it seems like everytime I have an problem it's always some setting in that file....
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP Session Sharing

Post by Eran »

Different browsers have different sessions. That is a fact. They store it at completely different physical locations. Unless you are using a custom installation of multiple IE or something like that.
Please show us your code.
williamshen25
Forum Newbie
Posts: 10
Joined: Fri Sep 03, 2010 5:45 pm

Re: PHP Session Sharing

Post by williamshen25 »

ok here is an example code I used.

Code: Select all

<?php
session_start();

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

echo $_SESSION['var'];

?>
If I open two browser, on the first one goto "index.php?var=a" . It display "a". On the second one I just go to "index.php" , it also display "a".
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP Session Sharing

Post by Eran »

The code you use sets the session variable whenever the $_GET variable exists. Since you use the same URL in all browsers - you get the same results (as the URL contains the same $_GET variable)
williamshen25
Forum Newbie
Posts: 10
Joined: Fri Sep 03, 2010 5:45 pm

Re: PHP Session Sharing

Post by williamshen25 »

pytrin wrote:The code you use sets the session variable whenever the $_GET variable exists. Since you use the same URL in all browsers - you get the same results (as the URL contains the same $_GET variable)
no pytrin, the second browser I used index.php without any query attatching to it, so it shouldn't detect there is any $_GET variable.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP Session Sharing

Post by Eran »

Try clearing the cookies in both browsers and try again - you might have done it by accident before and now it remembers the state. What browsers are those by the way?
williamshen25
Forum Newbie
Posts: 10
Joined: Fri Sep 03, 2010 5:45 pm

Re: PHP Session Sharing

Post by williamshen25 »

I cleared the cookie and it still perform the same =( I am using IE 8 ....
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP Session Sharing

Post by requinix »

Different windows of the same browser will use the same cookies. That means the same session.
Different browsers will use different cookies. That means different sessions.
User avatar
Eran
DevNet Master
Posts: 3549
Joined: Fri Jan 18, 2008 12:36 am
Location: Israel, ME

Re: PHP Session Sharing

Post by Eran »

Are you using the same browser ... ?
User avatar
Jonah Bron
DevNet Master
Posts: 2764
Joined: Thu Mar 15, 2007 6:28 pm
Location: Redding, California

Re: PHP Session Sharing

Post by Jonah Bron »

If you're just using multiple windows of the same browser, there is no way I know of to prevent them sharing that session across windows. Why would you want to prevent that?
Post Reply