I've just made a new and more secure login system for my website. Although it has been tested there's probably flaws with it. What do people think?
Login:
<?
$title = "Logging in";
require_once('../includes/mysql_connection.php');
if(isset($_COOKIE['username'])){
//user isn't logged in
$username = mysql_escape_string($_POST['username']);
$password = md5($_POST['password']);
require_once('../includes/functions.php');
$result = mysql_execute("SELECT id FROM users WHERE username = '$username' AND password = '$password' LIMIT 1", "godofgod_p_bb1");
if(mysql_fetch_row($result)){
//user information is correct
if(isset($_POST['remember'])){
//if user wants to be remembered for 100 days
setcookie("username", $username, time()+3600*24*100, "/", ".godofgod.co.uk");
setcookie("password", $password, time()+3600*24*100, "/", ".godofgod.co.uk");
}else{
setcookie("username", $username, 0, "/", ".godofgod.co.uk");
setcookie("password", $password, 0, "/", ".godofgod.co.uk");
}
$direct = eregi("logout\.php",$_SERVER['HTTP_REFERER'])? "http://www.godofgod.co.uk" : $_SERVER['HTTP_REFERER'];
header("Location: $direct");
}else{
include('../template/header.php');
echo "Bad username or password.";
include('../template/footer.php');
}
}else{
include('../template/header.php');
echo "You are already logged in.";
include('../template/footer.php');
}
?>
Logout:
<?
$direct = eregi("(login|management|logout)\.php",$_SERVER['HTTP_REFERER'])? "http://www.godofgod.co.uk" : $_SERVER['HTTP_REFERER'];
header("Location: $direct");
setcookie("username", 0, time() - 1, "/", ".godofgod.co.uk");
setcookie("password", 0, time() - 1, "/", ".godofgod.co.uk");
//for those who do not redirect
include('../template/header.php');
echo "You have been logged out. Click <a href='$direct'>here</a> to be taken back.";
include('../template/footer.php');
?>
Extra code included on every page for security:
if(isset($_COOKIE['username']) && isset($_COOKIE['password'])){
$username = $_COOKIE['username'];
$password = $_COOKIE['password'];
$result = mysql_execute("SELECT id FROM users WHERE username = '$username' AND password = '$password'", "godofgod_p_bb1");
if(mysql_fetch_row($result)){
// a future change will include conversion of these variables into constants
$user_log = 1;
$user_name = $username;
}
}
mysql_execute() for those that are interested:
function mysql_execute($sql = 0, $database = 0){
if($database){
mysql_select_db($database) or die(mysql_error());
}
if($sql){
$result = mysql_query($sql);
if(!$result){
if(USER_EMAIL){
$dynamic_mess = " You should be emailed if the problem is fixed.";
$from = "From: " . USER_MAIL;
}else{
$from = "";
}
$mail = mail("email", "godofgod.co.uk database error", mysql_error() . "\n\n The following query has caused a problem: \n\n$sql\n\n page: " . $_SERVER['REQUEST_URI'], $from);
if($mail){
$dynamic_mess = "A full error report has been emailed to the administrator." . $dynamic_mess;
}else{
$dynamic_mess = "There has been an error contacting the administrator about the error. Please send the full error messaage to email so the error can be fixed.";
}
die(mysql_error() . "<br><br> The following query has caused a problem: <br><br>$sql<br><br>" . $dynamic_mess);
}
}
return $result;
}