p2p.wrox.com Forums

p2p.wrox.com Forums (http://p2p.wrox.com/index.php)
-   BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 (http://p2p.wrox.com/forumdisplay.php?f=123)
-   -   disappearing cookie (http://p2p.wrox.com/showthread.php?t=20443)

droth October 26th, 2004 09:27 PM

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>

droth October 27th, 2004 04:25 PM

Ok. I found the problem. Here's the movie1.php non-working code:

---------------------------------------------------------
<?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>
-----------------------------------------------------------------

and here is the working code:

-------------------------------------------
<?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>
-----------------------------------------------------------------

What?
You can't find the difference?
Here it is:

href='http://localhost/BeginningPhpApacheMySqlDevelopment/chap2/moviesite.php?favmovie=$myfavmovie'>";

href='http://localhost/BeginningPhpApacheMySqlDevelopment/Chap2/moviesite.php?favmovie=$myfavmovie'>";

"Chap2" vs. "chap2". That's it.

The cookie file is named "Chap2/" on creation. But moviesite.php was being called using "chap2/", so in moviesite.php a non-existent cookie file "chap2/" is searched for.

What are the rules governing case sensitivity of cookie files?

As far as I can see, if "chap2/movie1.php" is entered into the browser (IE6) it is changed to "Chap2/movie1.php" before loading, and that is what's used to generate the cookie file. Then if "chap2/moviesite.php" is used in movie1.php, "chap2/moviesite.php" appears in the address line when moviesite.php is loaded, and cookie file "chap2" is not found. If I place my cursor onto the address line of IE6 it selects the entire address. Then when I press enter to reload the page, IE6 reformats the address to use "Chap2" before reloading, and magic - the cookie file is found and the username is displayed.

Mark my words. Computers are truly the work of the devil.

fhzaki October 27th, 2004 10:47 PM

hi, thanks. You're save my day. you so genius.


droth October 28th, 2004 09:24 AM

There is much yet to be answered. For example, why does the session ID appear in the cookie array even if the lower case "chap2" is used?


All times are GMT -4. The time now is 10:56 PM.

Powered by vBulletin®
Copyright ©2000 - 2019, Jelsoft Enterprises Ltd.
© 2013 John Wiley & Sons, Inc.