PHP session variable changes between pages

后端 未结 4 1909
一个人的身影
一个人的身影 2021-01-06 04:09

I have a session variable that I set like this:



        
相关标签:
4条回答
  • 2021-01-06 04:38

    Looks weird. That first chunk of code that resets the token must have been run again somehow.

    0 讨论(0)
  • 2021-01-06 04:44

    The only solution I can think of is that you are making a second request to the first page without knowing it. You should probably check your apache access log for this second access...

    Making a simple request counter would be another solution to check this:

    $_SESSION['counter'] = isset($_SESSION['counter'])? $_SESSION['counter'] +1 : 0;
    
    0 讨论(0)
  • 2021-01-06 04:53

    Completely stupid mistake on my part. I had some empty <img> tags in there that were causing the extra requests. facepalm Sorry everyone, problem solved. Thanks for your help!!

    0 讨论(0)
  • 2021-01-06 05:01

    You will notice that every time you revisit the first page, your session variable will change. Since it works for a constant string, 'example', I will assume that you revisit page 1 to view what is stored there.

    A fix could be checking to ensure that that session variable is not set before you set it again. i.e.

    <?php
    session_start();
    if(!empty($_SESSION['token'])){
        $token = md5(uniqid(rand(), true));
        $_SESSION['token'] = $token;
    }
    print $_SESSION['token'];
    ?>
    

    This chunk of code should work as expected.

    0 讨论(0)
提交回复
热议问题