View Single Post
  #1 (permalink)  
Old October 26th, 2004, 09:27 PM
droth droth is offline
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default disappearing cookie

I'm on chapter 2 pg 56-57 and having trouble passing the username cookie from movie1.php to moviesite.php using setcookie(). Any help would be appreciated.

The username value 'Joe' appears in the $_COOKIE array in the context of movie1.php but when the link to moviesite.php is clicked and the moviesite.php screen is displayed, the username is not in the $_COOKIE array anymore.

I've added some debugging code to try to see what's wrong. Here's what I get when movie1.php is displayed in my browser (I have to reload once to get the username in the cookie array, which is expected):

----------------------------------
setcookie(\'username\, 'Joe', 1098837934) succeded
cookie will expire in 3600 seconds
Session Array ( [authuser] => 1 )
Cookie Array ( [username] => Joe [PHPSESSID] => 4ce78919a88ed1cd21394963a89f1618 )
Click here to see information about my favourite movie
----------------------------------

Then when I click the link to moviesite.php here's what's displayed:

----------------------------------
Session Array ( [authuser] => 1 )
Cookie Array ( [PHPSESSID] => 4ce78919a88ed1cd21394963a89f1618 )
Welcome to our site, !
My favourite movie is Life of Bryan!
My movie rating for this movie is: 5
----------------------------------

As you can see, the username is gone from $_COOKIE, only session id remains. The session id is the same in both cases, and the cookie has not expired. Here's what's in my php.ini file:

output_buffering = 4096
session.save_handler = files
session.save_path = "E:/temp/Temporary Internet Files/session"
session.use_cookies = 1
session.use_only_cookies = 1
session.name = PHPSESSID
session.auto_start = 0
session.cookie_lifetime = 0
session.cookie_path = /
session.cookie_domain =
session.serialize_handler = php
session.gc_probability = 1
session.gc_divisor = 100
session.gc_maxlifetime = 1440
session.bug_compat_42 = 1
session.bug_compat_warn = 1
session.referer_check =
session.entropy_length = 0
session.entropy_file =
session.cache_limiter = nocache
session.cache_expire = 180
session.use_trans_sid = 0

here's what's in the php files:

-------------------------------
movie1.php
-------------------------------
<?php
session_start();
$t=time()+3600;
if ( setcookie('username', 'Joe', $t) == TRUE ) {
   echo "setcookie(\'username\, 'Joe', $t) succeded";
   echo "<br>";
   $tt = $t-time();
   echo "cookie will expire in $tt seconds";
   echo "<br>";
}

echo 'Session ';
//reset($_SESSION);
print_r($_SESSION);
echo "<br>";

echo 'Cookie ';
//reset($_COOKIE);
print_r($_COOKIE);
echo "<br>";

//$_SESSION['username']='Joe12345';
$_SESSION['authuser']=1;
?>

<html>
<head>
 <title>Find my favourite movie!</title>
</head>
<body>
  <?php
  $myfavmovie = urlencode("Life of Bryan");
  echo "<a href='http://localhost/BeginningPhpApacheMySqlDevelopment/chap2/moviesite.php?favmovie=$myfavmovie'>";
  echo "Click here to see information about my favourite movie";
  echo "</a>";
  ?>
</body>
</html>

--------------------------
moviesite.php
--------------------------
<?php
  session_start();
  //check to see if user has logged in with a valid password
  if ($_SESSION['authuser']!=1) {
    echo "Sorry, but you don't have permission to view this page - you loser!";
    exit();
  }
  echo 'Session ';
  //reset($_SESSION);
  print_r($_SESSION);
  echo "<br>";

  echo 'Cookie ';
  //reset($_COOKIE);
  print_r($_COOKIE);
  echo "<br>";
?>

<html>
<head>
  <title>My Movie Site - <?php echo $_REQUEST['favmovie'] ?> </title>
</head>
<?php
  echo "Welcome to our site, {$_COOKIE['username']}";
  //echo $_COOKIE['username'];
  echo "! <br>";
  echo "My favourite movie is {$_REQUEST['favmovie']}!";
  //echo $_REQUEST['favmovie'];
  echo "<br>";
  $movierate=5;
  echo "My movie rating for this movie is: ";
  echo $movierate;
?>
</body>
</html>
Reply With Quote