<?php
//Authentication Script, log a user in or out of the system
//Include required functions
require_once('sessionfunc.php');
//Create shorter names for variables
@$username= $_POST['username'];
@$passwd= $_POST['passwd'];
//Clear errorcode variable if it already exists
if(isset($_GET['errcode']))
{ unset($_GET['errcode']); }
session_start();
//Execute for login
if ($_POST['action']=="Login")
{
//Check if either field is empty
if ((empty($username)) || (empty($passwd)))
{
$url= $_SESSION['pageurl']."?errcode=1";
header('Location:' . $url);
}
else
{
//Connect to the database
$handle = dbhandle();
//cannot use dberror as not using $result
if (!$handle)
{
$url= $_SESSION['pageurl']."?errcode=6";
header('Location:' . $url);
exit;
}//otherwise connect to database
else {$connect = mysql_select_db('a7464386_justbe', $handle);}
//Check if provided username and password match
$result = mysql_query("SELECT * FROM user WHERE username= '$username' AND passwd = PASSWORD('$passwd')");
//Check for database error
dberror($result);
//If result is found, true otherwise username or password is wrong
if (mysql_num_rows($result)>0)
{
$_SESSION['validuser'] = $username;
$url = $_SESSION['pageurl'];
header('Location:' . $url);
exit;
}
else
{
$url = $_SESSION['pageurl']."?errcode=12";
header('Location:' . $url);
exit;
}
}
}
//Execute for logout
else
{
//No longer passes as logged in but rest of session data can remain until expiry
unset($_SESSION['validuser']);
$url = $_SESSION['pageurl'];
echo $url;
header('Location:' . $url);
exit;
}
?>