Good evening from beautiful Greece,
you have some errors in your initial scripts (not closing html tags, should increase poor cookie's lifetime and echo in function and not in body in 2nd file) made some modifications and worked ok in me. Note that I renamed listing15.7.php->help1.php, listing15.8.php->help2.php, listing15.9.php->help3.php, testDB -. testDB1 and that I also don't use password on root in mysql. So your files are as following:
------------------------------------------------ help1.php -------------------------------------------------
<html>
<head>
<title>Listing help1 User Login Form</title>
</head>
<body>
<H1>Login Form</H1>
<FORM METHOD="POST" ACTION="help2.php">
<P>
<STRONG>Username</STRONG><BR>
<INPUT TYPE="text" NAME="username"></p>
<P>
<STRONG>Password</STRONG><BR>
<INPUT TYPE="password" NAME="password"></p>
<P>
<INPUT TYPE="SUBMIT" NAME="submit" VALUE="Login"></P>
</FORM>
</body>
</html>
------------------------------------------------ help2.php -------------------------------------------------
<?php
//check for required fields from the form
if ((!$_POST['username']) || (!$_POST['password'])) {
header("Location: help1.php"
;
exit;
}
//connect to server and select database
$conn = mysql_connect("localhost", "root", ""
or die(mysql_error());
mysql_select_db("testDB1",$conn) or die(mysql_error());
//create and issue the query
$sql = "select f_name, l_name from auth_users where username = '$_POST[username]' AND password = password('$_POST[password]')";
$result = mysql_query($sql,$conn) or die(mysql_error());
//get the number of rows in the result set; should be 1 if a match
if (mysql_num_rows($result) == 1)
{
//if authorized, get the values of f_name l_name
$f_name = mysql_result($result, 0, 'f_name');
$l_name = mysql_result($result, 0, 'l_name');
//set authorization cookie
$inTwoMonths = 60 * 60 * 24 * 60 + time();
setcookie("auth","1", $inTwoMonths);
//prepare message for printing, and user menu
$msg = "<P>$f_name $l_name is authorized!</p>";
$msg .= "<P>Authorized Users' Menu:";
$msg .= "<ul><li><a href=\"help3.php\">secret page</a></ul>";
echo "$msg";
}
else
{
//redirect back to login form if not authorized
header("Location: help1.php"
;
exit;
}
?>
<html>
<HEAD>
<TITLE>Listing help2.php User Login </TITLE>
</HEAD>
</html>
------------------------------------------------ help3.php -------------------------------------------------
<?php
//if ($_COOKIE[auth] == "1"
{
if (isset($_COOKIE['auth']))
{
$msg = "<p>You are an authorized user.</p>";
}
else
{
//redirect back to login form if not authorized
header("Location: help1.php"
;
exit;
}
?>
<html>
<head>
<title>Listing 15.8 Accessing a restricted page </title>
</head>
<body>
<?php print "$msg"; ?>
</body>
</html>
------------------------------------------------ create_table.sql ------------------------------------------------
use testdb1;
create table auth_users
(
id int not null primary key auto_increment,
f_name varchar(50),
l_name varchar(50),
email varchar(150),
username varchar(25),
password varchar(75)
);
insert into auth_users values (null, 'john', 'doe', 'john@doe.com', 'jdoe', password('doepass'));
Now it works ok....
ps regarding greek fonts now they play kind of ok because if i use utf8 I can see greek fonts coming from php script (echos for example) and not the ones from mysql (?????? instead) and if I use iso-8859-7 exactly the opposite occurs. I suppose it's a wamp's bug and hopefully it will be fixed the soonest possible!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!