Wrox Programmer Forums
| Search | Today's Posts | Mark Forums Read
BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6
This is the forum to discuss the Wrox book Beginning PHP, Apache, MySQLWeb Development by Michael K. Glass, Yann Le Scouarnec, Elizabeth Naramore, Gary Mailer, Jeremy Stolz, Jason Gerner; ISBN: 9780764557446
Welcome to the p2p.wrox.com Forums.

You are currently viewing the BOOK: Beginning PHP, Apache, MySQL Web Development ISBN: 978-0-7645-5744-6 section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old October 26th, 2004, 09:27 PM
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>
  #2 (permalink)  
Old October 27th, 2004, 04:25 PM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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.
  #3 (permalink)  
Old October 27th, 2004, 10:47 PM
Registered User
 
Join Date: Oct 2004
Location: ipoh, perak, Malaysia.
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Send a message via Yahoo to fhzaki
Default

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

  #4 (permalink)  
Old October 28th, 2004, 09:24 AM
Registered User
 
Join Date: Oct 2004
Location: , , .
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?


Similar Threads
Thread Thread Starter Forum Replies Last Post
Disappearing ListView Columns nzakas General .NET 1 June 1st, 2006 04:43 PM
disappearing spaces gezi XSLT 0 March 26th, 2006 02:23 AM
Session variables are disappearing? LamivNahdus PHP How-To 0 November 25th, 2005 11:27 AM
Disappearing wizards BlacKnight Access 3 January 19th, 2005 04:35 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.