Page Hanging When Using $_SESSION
Posted: Sun Mar 13, 2011 12:56 pm
Hi
I recently added the following code to the top of every page on my ecommerce website (by putting it into a PHP include file and including it as the first statement of every page). The purpose of the code is to track the last 20 URLs visited, so I can do better navigation with the "back" button on each page, in case the user has stepped off-site during their visit (I don't want the back button to take them back offsite).
Tested everything and it all seemed to work fine. However, when I was using the site a few hours later, I noticed that all the pages were not loading - they seemed to be "hanging" on load, so the page never got displayed. I hurriedly removed the code that I had added, and all is OK again, but I would like to use this code if I can get it working reliably.
Here is the code - can anyone think why it might case a hang on page load, under certain conditions?
Cheers,
Andy
I recently added the following code to the top of every page on my ecommerce website (by putting it into a PHP include file and including it as the first statement of every page). The purpose of the code is to track the last 20 URLs visited, so I can do better navigation with the "back" button on each page, in case the user has stepped off-site during their visit (I don't want the back button to take them back offsite).
Tested everything and it all seemed to work fine. However, when I was using the site a few hours later, I noticed that all the pages were not loading - they seemed to be "hanging" on load, so the page never got displayed. I hurriedly removed the code that I had added, and all is OK again, but I would like to use this code if I can get it working reliably.
Here is the code - can anyone think why it might case a hang on page load, under certain conditions?
Cheers,
Andy
Code: Select all
<?php session_start();
// If Session has not been initiated before
if ($_SESSION==NULL)
{
// Set history count as 0
$_SESSION['count']=0;
// Store current page address in the history as 0th entry
$pageURL = $_SERVER["REQUEST_URI"];
$_SESSION['pageadd'][0]=$pageURL;
}
// If Session has been initiated before
else
{
// Store current page address in history list as count-th entry
$pageURL = $_SERVER["REQUEST_URI"];
// Search from end of array backwards for this URL - if we find a match, we just set our array counter to that place
for ($i = $_SESSION['count']; $i >= 0; $i--)
{
if ($_SESSION['pageadd'][$i] == $pageURL)
{
$_SESSION['count'] = $i;
break;
}
}
// Check for just refreshing same page, dont store it again
if ($_SESSION['pageadd'][$_SESSION['count']] != $pageURL)
{
// Check for array up to max size
if ($_SESSION['count'] == 20)
{
array_shift($_SESSION['pageadd']);
$_SESSION['count'] = 19;
}
// Increase counter pointing to current page
$_SESSION['count']=$_SESSION['count']+1;
$_SESSION['pageadd'][$_SESSION['count']]=$pageURL;
}
}
?>