Jump to content

Not staying logged in???


cturner

Recommended Posts

When I test the following code and then go to another page it says that I am not logged in. Can someone please tell me how I can fix this problem? Thanks in advance.

[code]
require "config.php";
$admin_username = $_POST['admin_username'];
$admin_password = $_POST['admin_password'];
if (isset($_POST['login'])) {
md5($admin_password);
$check = "SELECT COUNT(*) FROM tbl_admin WHERE admin_username='". mysql_real_escape_string($admin_username) . "' AND admin_password ='". mysql_real_escape_string($admin_password) . "'";

$query = mysql_query ( $check ) or die ("Query did not work because: ".mysql_error());

if (mysql_num_rows ($query)) {
$row = mysql_fetch_assoc($query);
setcookie ("admin_username", $row['admin_username']);
setcookie ("admin_password", $row['admin_password']);
header('Location: products.php');
exit;
} else {
    echo "Sorry, don't know who you are.";
}
}
mysql_close();
[/code]
Link to comment
Share on other sites

When you say "go to another page" I am assuming that you are going to anothr page in your site and are running the validatin check above on that page as well. If that is the case then that explains the problem. When you submit a form, the values of the form (i.e. the $_POST values) are only available on the next page.

I suggest you save the login information to session variables. Thenthe validation script should first look to see if those exist and validate against that. If they do not exist, then see if the $_POST values exist and attempt to validate against those.
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.