...

View Full Version : Undefined index



HeathP123
11-26-2011, 12:30 AM
I'm getting this error only when I click remember me for my log in, close my browser, and return to the index (which is directed right to the members page) for "HELLO, $username". Otherwise, when I click remember me and sign in, my index works fine.

login.php:


<?php

include 'functions.php';


$username = $_POST['username'];
$password = $_POST['password'];
$rememberme = $_POST['rememberme'];

if ($username&&$password)
{

$login = mysql_query("SELECT * FROM users WHERE username='$username'");
$numrows = mysql_num_rows($login);

if ($numrows!=0)
{

while ($row = mysql_fetch_assoc($login))
{
$dbpassword = $row['password'];
$dbusername = $row['username'];
if ($username==$dbusername&&$password==$dbpassword)
{
$loginok = TRUE;

if ($loginok==TRUE&&$rememberme="on")
{
setcookie("username", $username, time()+7200);
$_SESSION['username']=$username;
header("Location: members.php");
exit();

} else if ($loginok==TRUE&&$rememberme=="") {
$_SESSION['username']=$username;

header("Location: members.php");
exit();
}
else
$loginok = FALSE;
header("Location: incorrect.html");
exit();
}
else
header("Location: incorrect.html");
exit();
}
}
}
else
header("Location: incorrect.html");
exit();



?>


members.php:

<?php
include 'functions.php';

if (!loggedin())
{
header("Location: index.php");
exit();

}

if ($_SESSION['username'])
{
echo "HELLO, ".$_SESSION['username']."!";
}

?>

mlseim
11-26-2011, 12:41 AM
PHP SESSIONS are only active until you close your browser.
After that, they are discarded ... gone forever.

You need to use cookies instead.

But don't save usernames and passwords with cookies.
Create a new column in your database called "tokenKey" and
each time a person wants to be remembered (remember me on this PC),
you generate a random 10 character key and save it BOTH in the cookie
and in the row for that user.

Any time that user visits the site, if a cookie exists and there is a tokenKey,
it will find that user's row in the database, and log them in.

That's why you always see the warning ...
If your computer is shared by others, do not click "remember me on this computer".

Once logged in, re-generate a new tokenKey and resave the cookie and update the column.

... and also, at the top of every script that uses SESSION,
you must start out like this:
<?php
session_start();

If your "function.php" does that already, you're covered.


.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum