Jump to content

Recommended Posts

Hi, I'm trying to store the last 5 pages a user visited on my website and i can't seem to get it working. I've found a couple of pages that kind of tell me what i thought i need, but they dont seem to be correct.

 

Anyways heres what i've tried;

if(isset($_COOKIE['history']))
{
// if theres no cookie set yet, then set it with the current page.
// first we create an array
        $HistoryArray = array($_SERVER['PHP_SELF']);
// next we set the cookie with serialized() array
setcookie("history", serialize($HistoryArray), time()+(60*60*24*31), "/");

}
else
{
// if we already have a cookie set we want to get the data from it, add that to an array and reset the cookie
// first we unserialize() the cookies data
       $HistoryArray = unserialize($_COOKIE['history']);
// next we add to the array
$HistoryArray[] = array($_SERVER['PHP_SELF']);

       if(count($HistoryArray) > 5)
{
	// if theres more than 5 rows in the array then we want to remove the first row
	array_shift($HistoryArray);
	// finally we set the new cookie with the updated page
}
setcookie("history", serialize($HistoryArray), time()+(60*60*24*31), "/");
}

I have this in an include thats present in every page in my site

 

And then a test page i've made to try and see if this actually worked looks like this;

$NumCookies = count(unserialize($_COOKIE['history']));

echo "Num Cookies: ".$NumCookies."<br />";

for($i = 1; $i < $NumCookies + 1; $i++)
{
echo "Cookie number ".$i." : ".unserialize($_COOKIE['history'][$i])."<br />";
}

 

To me this logically seems to make sense right?

I only ever get one row listed no matter how many pages i go to on my website.

 

 

I also tried something similar to this with a manually created array just so i could test if my test page was even working. This almost worked, using the same code as above it counts the number of elements in the array correctly and loops through it but the value of the cookie is always blank.

 

What am i doing wrong, anyone know?

 

cheers

Link to comment
https://forums.phpfreaks.com/topic/240203-arrays-in-cookies/
Share on other sites

EDIT: I have just changed one small bit:

if(isset($_COOKIE['history']))

to

if(!isset($_COOKIE['history']))

 

which was just a sily mistake on my part. It does now show 5 rows (once i've visited 5 pages).

I am still however having problems actually getting that data, its still coming up blank

Link to comment
https://forums.phpfreaks.com/topic/240203-arrays-in-cookies/#findComment-1233828
Share on other sites

This:

// next we add to the array
$HistoryArray[] = array($_SERVER['PHP_SELF']);

Probably should be:

// next we add to the array
$HistoryArray[] = $_SERVER['PHP_SELF'];

And to display, probably:

$HistoryArray = unserialize($_COOKIE['history']);
$NumCookies = count($HistoryArray);

echo "Num Cookies: ".$NumCookies."<br />";

foreach($HistoryArray as $value) {
   echo $val . '<br />';
}

Link to comment
https://forums.phpfreaks.com/topic/240203-arrays-in-cookies/#findComment-1233894
Share on other sites

This:

// next we add to the array
$HistoryArray[] = array($_SERVER['PHP_SELF']);

Probably should be:

// next we add to the array
$HistoryArray[] = $_SERVER['PHP_SELF'];

Ah yeah i noticed this mistake myself shortly after posting.

And to display, probably:

$HistoryArray = unserialize($_COOKIE['history']);
$NumCookies = count($HistoryArray);

echo "Num Cookies: ".$NumCookies."<br />";

foreach($HistoryArray as $value) {
   echo $val . '<br />';
}

 

This part worked a treat, thanks.

As you can probably tell im not all that great with arrays, i forgot this was how to go about printing them.

 

Cheers

Link to comment
https://forums.phpfreaks.com/topic/240203-arrays-in-cookies/#findComment-1234215
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.