Upon refreshing http://mydomain.com, it will generate a random ID to display on the page.
For example, http://mydomain.com generates 54 the first time, and upon reloading, 112, etc.
I’d like to save each of the randomly generated IDs to a session, so each time it reloads, I can go back to the last one. For example, the first time it saves 54 to a session, and when http://mydomain.com reloads and generates 112, I can link back to the 54.
I can’t use HTTP_REFERER or REQUEST_URI, so I tried to work on my own version, but it’s only saving it once. I can’t figure out how to update it upon viewing the next ID.
if(empty($_SESSION['lastURL'])) {
$_SESSION['lastURL'] = $submissionId; // $submissionId is randomly generated
} else {
echo $_SESSION['lastURL'];
}
Is my current code. Where should I update the session to store the next ID?
Turns out the reason it kept misfiring was because it kept firing an additional ajax request, so I was getting a randomly generated id each time.
I (with the help of a friend) figured out by using the following code, and setting it before it randomly fires each time:
if(!isset($_SERVER['HTTP_X_REQUESTED_WITH']) || strtolower($_SERVER['HTTP_X_REQUESTED_WITH']) != 'xmlhttprequest'){
// Not an ajax request - just a normal page load
$_SESSION['lastid'] = $_SESSION['currentid'];
$_SESSION['currentid'] = $submissionId;
}
You’re only setting
$_SESSION['lastURL']once because it is onlyempty()once. You’ll need to update this value whenever you generate a new ID (thus making it the new “old” one).