开发者

PHP Sessions in tab

consider this problem

  1. In step 1 user selects a product i am storing $_SESSION['id'] = 55;
  2. Now user moves to step 2 and opens another product in a new tab.
  3. Now moves to that tab It's now step 1 for the product2
  4. Now $_SESSION开发者_运维百科['id'] is set 56;
  5. User moves tab 1 and continues to step 3
  6. But now $_SESSION['id'] = 56 and not 55

How can i prevent this. This is a cooked up problem so would like a generalized solution.


Sessions are site wide, so if you change the same session in one tab, it will be changed in the others. That is why sessions are used for site wide logins, as they don't change from window to window.

You should look into using Cookies or multiple sessions, but you cant change the same sessions on a tab by tab basis.


In step 1 user selects a product i am storing $_SESSION['id'] = 55;

Now user moves to step 2 and opens another product in a new tab.

Now moves to that tab It's now step 1 for the product2

Now $_SESSION['id'] is set 56;

User moves tab 1 and continues to step 3

But now $_SESSION['id'] = 56 and not 55

How can i prevent this

Easy - you don't set $_SESSION['id'] to 56. But in all seriousness, you may want to reconsider how you go about solving your problem. Putting 'id' in session allows for only one product at a time. As @Gumbo pointed out, why don't you put the ID into the URL?

Alternatively, depending on what your exact problem is, you may want to consider setting up your session variable like $_SESSION['products'][$product_number] such that $product_number contains the step you're on.

Consider the following example:

$_SESSION['products'][55]=2; // we're on step 2 here $_SESSION['products'][56]=1; // we're on step 1 here

On each page find out if $product_number is !in_array()==false and then retrieve the value to determine what step you're on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜