I got a problem with an authorization code. The login works fine. But my cookie does not see, to be working.
Here's the login code:
<?php
//check for required fields from the form
if ((!$_POST[username]) || (!$_POST[password])) {
header("Location: login.php"
;
exit;
}
//connect to server and select database
$conn = mysql_connect("localhost","",""
or die(mysql_error());
mysql_select_db("login",$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
setcookie("auth", "1", 0, "/", "localhost", 0);
//prepare message for printing, and user menu
$msg = "<p>$f_name $l_name is authorized!</p>";
$msg .= "<p>Authorized users menu:</p>";
$msg .= "<ul><li><a href=\"restcheck.php\">secret page</a></ul>";
} else {
//redirect back to login form if not authorized
header("Location: login.php"
;
exit;
}
?>
<html>
<head>
<title>User Authorized</title>
</head>
<body>
<? print "$msg"; ?>
</body>
</html>
and the testing for auth cookie:
<?php
if ($_COOKIE[auth] == "0"
{
$msg = "<p>You are an authorized user.</p>";
} else {
//redirect back to login form if not authorized
header("Location: login.php"
;
exit;
}
?>
<html>
<head>
<title> You have entered a restricted zone!</title>
</head>
<body>
<?php print "$msg"; ?>
</body>
</html>
Here's the login code:
<?php
//check for required fields from the form
if ((!$_POST[username]) || (!$_POST[password])) {
header("Location: login.php"

exit;
}
//connect to server and select database
$conn = mysql_connect("localhost","",""

or die(mysql_error());
mysql_select_db("login",$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
setcookie("auth", "1", 0, "/", "localhost", 0);
//prepare message for printing, and user menu
$msg = "<p>$f_name $l_name is authorized!</p>";
$msg .= "<p>Authorized users menu:</p>";
$msg .= "<ul><li><a href=\"restcheck.php\">secret page</a></ul>";
} else {
//redirect back to login form if not authorized
header("Location: login.php"

exit;
}
?>
<html>
<head>
<title>User Authorized</title>
</head>
<body>
<? print "$msg"; ?>
</body>
</html>
and the testing for auth cookie:
<?php
if ($_COOKIE[auth] == "0"

$msg = "<p>You are an authorized user.</p>";
} else {
//redirect back to login form if not authorized
header("Location: login.php"

exit;
}
?>
<html>
<head>
<title> You have entered a restricted zone!</title>
</head>
<body>
<?php print "$msg"; ?>
</body>
</html>