/    Sign up×
Community /Pin to ProfileBookmark

Recently View Cookie Help

Hello,

I am trying to create a feature on my site that will track which pages the user has gone to and then display the last 4 in an area called Recently Viewed.

I got a little bit starting with it, I basically am logging every view into a cookie on the users computer. I am stumbling at the part of making sure I dont have the same page twice in the listings.

What is the best way to do this? I was thinking of using an array to store the value from the cookie and then searching to see if the current page is already in the array. If it is then skip it, else set the cookie.

Here is my script so far:

[code=php]if ($n_pages > 0) {
if (isset($_COOKIE[“recentlyviewed”])) {
$store = $_COOKIE[“recentlyviewed”];
$store .= “/”.$current_category[“id”].”&”.$current_category[“name”];
//setcookie(“recentlyviewed”, urldecode($store), time()+36000, “/”);
echo $_COOKIE[“recentlyviewed”];
} else {
$store = $current_category[“id”].”&”.$current_category[“name”];
setcookie(“recentlyviewed”, urldecode($store), time()+36000, “/”);
}
}[/code]

NOTE: I am grabbing the artists ID and name and storing it in the cookie. I want to only keep 4 pages in the cookie.

Any ideas?

to post a comment
PHP

7 Comments(s)

Copy linkTweet thisAlerts:
@cohesiveMay 12.2006 — The easiest way to extract the info would be to get your cookie string into an array using explode:

[code=php]$arrPages = explode($_COOKIE['recentlyviewed'], '/');[/code]

Then what you have to do is iterate through the array and check if the page already exists, if it does then do nothing, if it doesn't then rotate the array values and add to the bottom, like this:

[code=php]
// Build the string for the current page to compare with
$thispage = $current_category["id"] . "&" . $current_category["name"];
// Check the fetched array against the current page
foreach($arrPages as $page) {
if ($thispage != $page) {
$intFound = 1;
}
}
// if not found, rotate the array and append the value to the last value
if (!$intFound) {
for ($x = 0; $x < count($arrPages); $x++) {
$arrPages[$x] = $arrPages[$x + 1];
}
$arrPages[3] = $thispage;
}
// iterate through the array to build the cookie string
for ($x = 0; $x < 4; $x++) {
$recentpages .= '/' . $arrPages[$x];
}
// set the cookie
setcookie('recentlyviewed', $recentpages, time() + 36000);
[/code]


this may or may not work (haven't tested it) and is probably not the most efficient way of going about it, but you get the picture.
Copy linkTweet thisAlerts:
@mididelightauthorMay 12.2006 — so once i get to 4 pages in the cookie and the user goes to another page, how do I remove the first key and add the current page to the end of the array?
Copy linkTweet thisAlerts:
@cohesiveMay 13.2006 — the second loop does that by rotating the each array value up and then setting the fourth (index 3) to the new page
Copy linkTweet thisAlerts:
@bokehMay 14.2006 — [code=php]<?php

function RecentPages($cookie_name, $quantity = 4)
{
$cookie_array = isset($_COOKIE[$cookie_name])
?(unserialize((get_magic_quotes_gpc())?stripslashes($_COOKIE[$cookie_name]):$_COOKIE[$cookie_name]))
: array() ;
setcookie(
$cookie_name,
serialize(array_values(array_slice(array_unique(array_merge(array($_SERVER['PHP_SELF']),$cookie_array)),0,$quantity))),
time()+86400
);
return array_slice($cookie_array, 0, $quantity);
}

#test it
print_r( RecentPages('cookie_name') );

?>[/code]
Copy linkTweet thisAlerts:
@hazadeFeb 16.2007 — I am having a similar problem as described above. Basically I have a form that sends a variable value ($ticker) to another page. What I am trying to do is take that value that is submitted and save it to a "Recently Submitted" section and have it list the last 5 submissions. I am having trouble because it submits the same variable each time and it is overwriting itself in the cookie. I have been struggling with the code and am unable to get it to work. Any suggestions?
Copy linkTweet thisAlerts:
@hazadeFeb 20.2007 — ttt
Copy linkTweet thisAlerts:
@hazadeFeb 22.2007 — nobody? I need help!
×

Success!

Help @mididelight spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...