I have a session variable that I set like this:
Looks weird. That first chunk of code that resets the token must have been run again somehow.
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;
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!!
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.