PDA

View Full Version : Cookie: Last Visited Pages


casbboy
7-27-04, 02:59 AM
Ok, I have it setup so a cookie is created every page and placed as the last visited page. So when a person comes to my site they can click the title of there last visited page and it will take them right there. However, I would like it to show the last three pages visited, in the order they were visited.

How can I setup cookies to do this and interchange their information?

Thanks
Ryan

zimmer3
7-27-04, 03:25 AM
setcookie("page3", $_COOKIE["page2"]);
setcookie("page2", $_COOKIE["page1"]);
setcookie("page1", $nowpage);

that;s a quickie answer, but I think it should work

casbboy
7-27-04, 04:08 AM
Thanks, but let me restate what I am trying to do...

Right now I have something like this...


$canlast = "Headline: ". $runn['title'] ."|getnews.php?id=". $runn['newsid'] ."&issuenum=". $runn['issue'] ."&department=". $runn['field'] ."";
setcookie("canlast",$canlast,time()+60*60*24*180,'/','.canmag.com');


This sets up one cookie that keeps track of the last page visited as well as the address to link back to that page (I explode it later).

Then, in the last pages visited box I have....

if ($_COOKIE['canlast']) {
$lvisitf = $_COOKIE['canlast'];
$lvisit = explode('|',$lvisitf);
} else {
$lvisit[0] = "No Pages Last Visited";
}


Then further down on the page I have this


echo "<a href=\"". $lvisit[1] ."\" id=\"last\" target=\"_blank\">". $lvisit[0] ."</a>";


My page is databased so always loads new data onto page depending on what is REQUESTED. I want the cookies to kick down when a new page is visited.

My main problem is the creation of three duplicate cookies if the user keeps refreshing the same page...

casbboy
7-27-04, 04:29 AM
OK, here is what I did. My major problem was not allowing the cookie to create a duplicate of itself (say a person hit the refresh button on a page causing the cookie to be made again and therefore have two copies of the same cookie). Since my pages are Databases and loaded through php I created this code to cover my options..


session_start();
if($_SESSION['nnw'] != $runn['newsid']) {
$cookie3 = $_COOKIE['ccookie2'];
$cookie2 = $_COOKIE['canlast'];
setcookie("ccookie3",$cookie3,time()+60*60*24*180,'/','.canmag.com');
setcookie("ccookie2",$cookie2,time()+60*60*24*180,'/','.canmag.com');

$canlast = "Headline: ". $runn['title'] ."|getnews.php?id=". $runn['newsid'] ."&issuenum=". $runn['issue'] ."&department=". $runn['field'] ."";
setcookie("canlast",$canlast,time()+60*60*24*180,'/','.canmag.com');
$_SESSION['nnw'] = $runn['newsid'];
}




Not that pretty... But it works by creating a session/page id for each different page. If that ID turns to be the same as the session ID, it does nothing to the cookies.

Ryan

B&T
7-27-04, 09:40 PM
setcookie("page3", $_COOKIE["page2"]);
setcookie("page2", $_COOKIE["page1"]);
setcookie("page1", $nowpage);

OK, then a minor adjustment:

if ($_COOKIE["page1"] != $nowpage) {
setcookie("page3", $_COOKIE["page2"]);
setcookie("page2", $_COOKIE["page1"]);
setcookie("page1", $nowpage);
}

That should solve the refresh problem.

casbboy
7-28-04, 12:11 AM
yeah,

My session idea worked and i'm happy with it.. but that one could be done too

Thanks
Ryan