Hi,
I've created a login/logout area and was wondering if it's ok from a security point of view? The password is run through MD5. Also please let me know if it can be improved in any way. If the login attempt fails, it takes you to a page called "check-login.php" and waits 5 secs before redirecting back to "login.php". I think this could be improved so it does the check on the same page and shows an error, possible using AJAX/jQuery, but I'm a but unsure how to do this.
login.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Login</title>
</head>
<body>
<form name="login" action="check-login.php" method="post">
<fieldset>
<legend>Login form</legend>
<label for="username">Username:</label>
<input type="text" name="username" id="username" />
<label for="password">Password:</label>
<input type="text" name="password" id="username" />
<input type="submit" value="Login" />
</fieldset>
</form>
</body>
</html>
db-connection.php
<?php
// setting variable for db connection
$host = "localhost";
$username = "root";
$password = "myPass";
$database = "myDatabase";
// connect to database
$conn = mysqli_connect("$host", "$username", "$password", "$database");
if (!$conn) {
die("Could not connect: " . mysqli_error());
}
?>
check-login.php
<?php
require_once 'db-connection.php';
// user and pass sent from login page
$username = $_POST['username'];
$password = $_POST['password'];
// encrypt password using md5
$encrypt_password = md5($password);
// sanatise data function
function cleanInput($data, $conn) {
if (get_magic_quotes_gpc()) {
$data = stripslashes($data);
$data = strip_tags($data);
$data = mysqli_real_escape_string($conn, $data);
} else {
$data = strip_tags($data);
$data = mysqli_real_escape_string($conn, $data);
}
return $data;
}
// sanatise data
$username = cleanInput($_POST['username'], $conn);
$password = cleanInput($_POST['password'], $conn);
$sql = "SELECT * FROM users WHERE username = '$username' and password = '$encrypt_password'";
$result = mysqli_query($conn, $sql);
// check for username and password if match found
$count = mysqli_num_rows($result);
if ($count == 1){
session_register("username");
session_register("password");
header('Location: securepage.php');
} else {
?>
<script language="javascript">
var time_left = 5;
var cinterval;
function time_dec(){
time_left--;
document.getElementById('countdown').innerHTML = time_left;
if(time_left == 0){
clearInterval(cinterval);
}
}
cinterval = setInterval('time_dec()', 1000);
</script>
<p style="color: red;"><strong>Incorrect username or password</strong></p>
<p>Redirecting back to login page in <span id="countdown">5</span></p>
<?php
header('refresh: 5; url=login.php');
}
?>
logout.php
<?php
session_start();
session_destroy();
?>
securepage.php
<?php
session_start();
if(isset($_SESSION['username'])){
// do this
} else { // if user not logged in
header('Location: login.php');
}
?>