Page 1 of 1

cookie problems

Posted: Thu Nov 12, 2009 12:55 am
by Burbage
I can't quite figure out where I'm going wrong, and perhaps someone here can help me.

I have a website that displays images from an array. The index number of the array (1-150) is returned by a session variable counting up for every page view. Easy so far. My problem is that I want the user to be able to get back to the place they were in the 150 images if they leave and return to the site later. So I thought of putting a cookie on their computer which the index page could look up when they come back. Does that make sense?

Anyway, every time the viewing page is loaded the session variable counter clicks on by one, loading the next image. What I want to do is give them a button to push to go to a page which sends a cookie with the session number on it. So the code for the cookie sending page is this:

Code: Select all

<?php
setcookie('bm_danny', 0, mktime(), "/");
session_start();
$_SESSION['page'] = $_SESSION['page']-1;
echo "page = ". $_SESSION['page']; 
$_COOKIE['bm_danny'] = &$_SESSION['page'];
echo "cookie = ". $_COOKIE['bm_danny']; 
?>
The echoes are there to report the numbers and the cookie is set no problem . But when I look the cookie up on the index page which is one level above the cook sending page, the value of the cookie is zero. This is the code on the index page. It reports zero in the cookie echo.

Anyone know where I'm going wrong?

Code: Select all

<?php
  echo "page = ". $_COOKIE['bm_danny']; 
  
if ($_COOKIE['bm_danny'] > 0) {
    echo '<p><a href="danny/page.php"> continue reading </a></p>';
    }
    else {
    echo '<p><a href="danny/first_page.php"> Read </a></p>';
}
?>

Re: cookie problems

Posted: Thu Nov 12, 2009 12:59 am
by Burbage
Thanks in advance by the way. My browser is playing up on the posting page so I couldn't really finsh that message of properly. Anyway, If I go drictely from the cookie sending page to the index page the value of the cookie changes from whatever the number is to zero. I am at a loss.

Thanks.