简体   繁体   中英

Can a browser store two different sets of SESSION variables at the same time?

On a fitness website I'm working on, I use php $_SESSION to recall the user's username across the various php scripts that my pages use.

I had a tab open in Chrome that was logged in to one of my testuser accounts on a fitness website I was working on.

As I wanted to test the Update BMI feature on another user account, I opened a second tab and login-ed to another testuser account. The feature worked successfully, and my second user BMI was updated in the database.

However, when I went back to my first tab and tried the feature for my first user, it updated the BMI for the second user, not the first. This alarmed me immediately. I refreshed the page and realised I was actually now logged into the second testuser account.

This must be because a browser cannot store two different set of session variables. Is there anyway to allow a browser to do so?

Session variables are 1.

$_SESSION['user'] = 1;

So throughout your website, value of user is 1.

Now if you change the value,

$_SESSION['user'] = 2;

Whole website will have user value as 2 in session.

You have faced that problem because you have not refreshed the page.

Same browser cannot have multiple values for same variable, but its possible in different browser.

So 2 sets of value for same session variable is not possible in same browser.

Working:

Each session created will have an unique id , and that unique id is stored in cookies. Cookies in turn are store in browser. Every time you make request for session variables to server, it looks up for the session id in cookies.

Hope this helped you. Any doubt, you can ask.

A browser can only work with one session at a time.

However you can open another browser or use a private session(Ctrl+Maj+P) to test another session without loosing the first one.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM