Jump to content

New Authorized User Issue


twilitegxa

Recommended Posts

My form lets me enter the user name and password, then the php script checks that it is an authorized user, and when it is, it displays a link to a secret page, but when I try to click the link, it redirects me to the log in screen again. What is making it do this? Here are the codes:

 

<?php
//check for required fields from the form
if ((!$_POST[username]) || (!$_POST[password])) {
header("Location: listing15.7.php");
exit;
}

//connect to server and select database
$conn = mysql_connect("localhost", "root", "")
or die(mysql_error());
mysql_select_db("smrpg",$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 value 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, "/", "yourdomain.com", 0);

//prepare message for printing, and user menu
$msg = "<p>Congratulations, $f_name $l_name, you are authorized!</p>";
$msg .= "<p>Authorized Users' Menu:";
$msg .= "<ul><li><a href=\"listing15.9.php\">secret page</a></ul>";

} else {

//redirect back to login if not authorized
header("Location: listing15.7.php");
exit;
}
?>
<html>
<head>
<title>Listing 15.8 User Login</title>
</head>
<body>
<?php print "$msg"; ?>
</body>
</html>

 

And the secret page php code:

 

<?php
if ($_COOKIE[auth] == "1") {
$msg = "<p>You are an authorized user.</p>";
} else {
//redirect back to login form if not authorized
header("Location: listing15.7.php");
exit;
}
?>
<html>
<head>
<title>Listing 15.8 Accessing a restricted page</title>
</head>
<body>
<?php print "$msg"; ?>
</body>
</html>

 

Can anyone tell me what is wrong?

Link to comment
Share on other sites

I probably will, I just needed to make sure everything worked right, because what it was doing made it seem like once they logged in, that it checked and they weren't authorized since it would redirect them to sign in again.

 

It was supposed to let them sign in, tell them they were authorized, and then let them click the "secret" link for onyl authorized users. But after they would try to click it, it would redirect as if saying they were not authorized, so I was confused.

 

It still redirects, even after I changed the domain name though. Does that mean anything?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.