Just like MADCAT, I am having problems getting setcookie() to work. I am following the author's code example exactly (PHP Fast and Easy Web Development by Julie Meloni- pretty good book, for a newbie!), and have failed to realize the problem.
My environment: WIN 2000 Pro 5.00.2195 SP2, IIS, IE 6.0, PHP 4.3.0 RC1 CGI mode, MySQL 3.23.49. I have not had any problems with other tutorials/code examples from the book, so my environment seems to be set up properly. The only problem I have had so far is with the two examples from Chapter 16: Using Cookies.
Scenario: I have a very simple login form, using <form method="POST" action="do_authuser.php"> to authenticate users.
Here is the do_authuser.php code:
<?php
//check for required fields
if ((!$_POST[username]) || (!$_POST[password])) {
header("Location: http://localhost/show_login.html");
exit;
}
$db_name = "test";
$table_name = "auth_users";
$connection = @mysql_connect("localhost", "*****", "****") or die(mysql_error());
$db = @mysql_select_db($db_name, $connection) or die(mysql_error());
$sql = "SELECT * FROM $table_name WHERE username = '$_POST[username]'
AND password = password('$_POST[password]')";
$result = @mysql_query($sql, $connection) or die(mysql_error());
//count number of rows returned by query
$num = mysql_num_rows($result);
if ($num != 0) {
$cookie_name = "auth";
$cookie_value = "ok";
$cookie_expire = "0";
//$cookie_domain = "127.0.0.1";
//setcookie($cookie_name, $cookie_value, $cookie_expire, "/", $cookie_domain, 0);
setcookie($cookie_name, $cookie_value, $cookie_expire);
$display_block = "
<p><strong>Secret Menu:</strong></p>
<ul>
<li><a href=\"secretA.php\">secret page A</a>
<li><a href=\"secretB.php\">secret page B</a>
</ul>";
} else {
header("Location: http://localhost/show_login.html");
exit;
}
?>
NOTE that I commented out the $cookie_domain and next setcookie() lines to try the "smaller cookie" approach dalecosp suggested.
Here is the secretA.php code (secretB.php is the exact same code):
<?php
if ($_COOKIE[auth] == "ok") {
$msg = "<p>Welcome to secret page A, authorized user!</p>";
} else {
header("Location: http://localhost/show_login.html");
exit;
}
?>
<html>
<head>
<title>Secret Page A</title>
</head>
<body>
<?php "$msg"; ?>
</body>
</html>
When I run the page, the field checks, and other stuff works ok- if I try to go directly to secretA.php without login I get pushed to the login form. When I enter an incorrect ID/Password I get the login form. If I type the correct ID/Password, I get the secret menu text, with links to secretA.php and secretB.php.
Before I shortened the cookie parameters, I would always get sent back to the login form. I never receive a cookie prompt, even though i know I have my prefs set right as any other site I get prompted, local as well as internet sites. However, once I shortened the cookie and tried the pages in my Mozilla browser, i get prompted for the cookie. This doesn't happen in IE 6.0. Once I shortened the cookie, either browser, I don't get sent back to the login form (which should indicated a cookie was found, right?) but I get the secretA.php page BLANK. I view source, and I see the HTML framework, but no body markup. The browser page is blank. When I see the setcookie prompt using my Mozilla browser I accept and do a search in my cookies folder and can't find the cookie, so I am guessing it isn't being created?
Hope this provides enough information to help out. I have been wracking my brain over this one for a couple hours now.
Can anyone provide suggestions as for why this doesn't worK?
Thank you!
ecco