Thanks for your reply.
Yes, I am using the same page to log in and out. What I am doing is having every page in my site call session_start(), connect to mysql and then call a function I have called checklogin, passing it a reference to the database connection object.
Here is my checklogin function
function checklogin(&$dbconn)
{
//checks whether there is a user currently logged in.
//If a valid user is not logged in or session has expired, displays empty page with login prompt.
if (isset($_SESSION['valid_user'])) //check if there is a user already logged in
{
if (isset($_POST['logout'])) //logout has been selected
{
unset($_SESSION['valid_user']);
unset($_SESSION['timeout']);
session_destroy();
$page = new Page;
$page->content = "<h3>Thankyou for using mysite</h3>";
$page->login = generatelogin();
$page->Display();
$dbconn->close();
exit;
}
if (time() < $_SESSION['timeout'] + TIMEOUT*60) //make sure they haven't timed out
{
$_SESSION['timeout'] = time(); //login is OK so reset timeout timer and return userid
return getuserid($dbconn);
}
$page = new Page; //login has expired so notify and display appropriate page
unset($_SESSION['valid_user']);
$page->content = '<h3>Your login has expired. Please log in again.<h3>';
$page->login = generatelogin();
$page->Display();
$dbconn->close();
exit;
}
if (isset($_POST['username'])) //there is no user logged in so check whether page was loaded by clicking login prompt
{
$username = trim($_POST['username']);
$password = $_POST['password'];
if (!$username)
{ //no username entered
$page = new Page;
$page->content = '<h3>You did not enter a username</h3>';
$page->login = generatelogin();
$page->Display();
$dbconn->close();
exit;
}
if (!$password)
{ //no password entered
$page = new Page;
$page->content = '<h3>You did not enter a password</h3>';
$page->login = generatelogin();
$page->Display();
$dbconn->close();
exit;
}
$query = 'select * from users where username = "'.$username.'"'; //get password from database
$result = $dbconn->query($query);
if ($result->num_rows == 0)
{
$page = new Page; //no return from database therefore username does not exist
$page->content = '<h3>Unknown username. Try again</h3>';
$page->login = generatelogin();
$page->Display();
$dbconn->close();
exit;
}
$userarray = $result->fetch_assoc();
if ($userarray['password'] != sha1($password))
{
$page = new Page; //password does not match
$page->content = '<h3>Incorrect password. Try again</h3>';
$page->login = generatelogin();
$page->Display();
$dbconn->close();
exit;
}
else
{
$_SESSION['valid_user'] = $username; //Yay! successful login
$_SESSION['timeout'] = time();
return $userarray['userid']; //return userid from user table
}
}
If there is a problem with the login the function generates a page with an error message and a login panel at the top. If the login validates, the function sets the relevant session variable and returns the $userid value of the logged in user. The page that called the function will then generate a page with a logout panel replacing the login panel. The login or logout button always calls the same page that it came from (i.e. if the current page is anywhere.php the link will be to anywhere.php). I'm not sure how having a separate logout page with a redirect will prevent people from going back to the page after login and resending the POST variables. Can anyone help me understand this further?
Thanks again.