Jump to content

Sessions and FireFox Tabs


zq29

Recommended Posts

While debugging a script of mine I have stumbled across something that appears to be quite a concern. I am working on a project that has a front end, and an admin backend.

I use FireFox and have the front end open in one tab, and the backend open in another. Naturally, the admin is protected with a user:password combo, so I have logged myself in - the username is stored in the session variable $_SESSION['username']. Now back in my other tab, working with the front end, I'm debugging a script and find the need to check a few session variables so run a print_r($_SESSION).

What do I see? [username] => 'SemiApocalyptic' in my front end.

The frontend has a shopping basket, product details are stored in $_SESSION['basket'] - I add a few things to the basket, then switch back over to my admin tab and click my 'Logout' button, this destroys all my session data to log me out of the admin with either unset($_SESSION) or $_SESSION = array() (I can't remember now - one of the two).

Switch back over to my front-end, refresh my basket, and all my basket data is empty.

Now my project structure is like this:

Frontend: [a href=\"http://localhost/site/\" target=\"_blank\"]http://localhost/site/[/a]
Backend: [a href=\"http://localhost/site/admin/\" target=\"_blank\"]http://localhost/site/admin/[/a]

I'm quite concerned how my two tabs are sharing the same session data, is this because they are on the same server; same 'base directory'; or something else? Can anyone offer any insight as to what is happening, how to prevent it or any other useful input? Should I be concerned?
Link to comment
Share on other sites

I guess they will share session data as long as they have the same session id - what may mean, if you are using cookies to propagate session_id, it will share the same session id if you open another instance of Firefox.

I think using session_set_cookie_params() you can create different cookies for them - so they'll have different session id's.

[a href=\"http://www.php.net/session_set_cookie_params\" target=\"_blank\"]http://www.php.net/session_set_cookie_params[/a]
Link to comment
Share on other sites

I see, but do you think this 'feature' of FireFox could be used to exploit peoples sites? Or is session data only stored between tabs if both URLs point to the same sever/domain? (I would both think, and hope that this [i]is[/i] the case) Might have to do some testing to find out, to be sure.
Link to comment
Share on other sites

!?!!

well i did some testing on this issue, and i can confirm that it does indeed pull the session info from one tab to another, if the pages are from the same website. But it does not appear to grab session info from tabs that do not contain other websites.

did the following code:
[code]
<?php
  session_start();
  
  foreach($_SESSION as $key => $val) {
    echo $key . " : " . $val . "<br>";
  }
?>
[/code]
i opened my ff browser and ran this script. nothing.

then i opened a tab and went to one of my login screens for my site, and logged in. then i went back to this script tab and refreshed, and behold, i see my session info dumped on the screen, from the other tab. these scripts were also ran from different directories, btw.

i opened a 3rd tab and tried logging in to several sites (including this one) and then refreshing the script again, and nothing (nothing new-session info will be there until the window is completely closed- that's another thing you need to remember when doing tabbed browsing!).

so it [i]appears[/i] that your session info is safe, as far as running a script from another site, with your site tabbed.

now as far as same-site tabs sharing the session.. well, you can name things more explicitely. as in, on your frontend, have 'cust_username' 'cust_password' etc.. as user info and 'admin_username' 'admin_password' as the admin info, instead of using the same variable name for both.

and if for some reason you need to run a loop on all of your session vars, if you seperate them with a specific prefix, you can throw in a condition that checks for that prefix.

hope this helps!
Link to comment
Share on other sites

The key here is to use session_name before session_start within your admin area ie:
[code]<?php
session_name('adminArea'] // MUST BE FIRST LINE
session_start(); //all session data created will now be tied to the adminArea session

//create your sess vars as normal

?>[/code]
Now in your frontend just start your session as normal:
[code]<?php
session_start();

//create session vars

?>[/code]
You'll see now that your sessions wont mix, all admin session are tied to a seperate session.

NOTE: you must have session_name('adminArea') before session_start in every file that uses sessions in your admin area.

Also the name of the session can be anything but can only contain letters

Hope that helps.
Link to comment
Share on other sites

Keep in mind that this isn't a firefox issue.

Browsers know nothing of sessions or session data (firefox's reference to sessions when dealing with when cookies expire eg: "end of session", is only about when the browser is closed). It only stores a cookie with the session id and sends it to the server as it would any other cookie. When it comes to the session id being passed via the url, again the browser treats it no differently than any other url with a query string.
Link to comment
Share on other sites

Ok, it all appears to make sense! I was under the assumption that new tabs in FireFox were treated as the equivilent of a new browser window in IE, where a new browser window creates its own new (seperate) session regardless of it being the same site or not.

I must admit, I was a bit quick off the mark to create this thread, but as you could imagine - It was a bit concerning to see session data from other tabs in my active tab! But thinking about it, and everyones comments here, it does make sense.

Thanks for the tip there wildteen88, I think I might implement that into my projects from now on, just to keep things tidy when debugging.

Thanks for your input guys :)
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.