Hi all!
I'm wondering if you could look at my login code and tell me how secure it is. I know it seems to work pretty well, I'm just not very good at picking out security hazards.
Main login.php page:
A simple form which submits to itself to check for both fields filled in and checks to see if both the username and password correspond to a user in the database. Both values are md5 encrypted.
If the credentials are correct, the user is redirected to set.php which sets the session and cookie.
set.php:
if(isset($_GET['usr'])){
$username = $_GET['usr'];
$password = $_GET['pss'];
$hour = time() + (3600*24*30);
setcookie('uid', md5($username), $hour);
setcookie('pss', $password, $hour);
$_SESSION['ses_user'] = md5($username);
$_SESSION['ses_pass'] = $password;
}
// Redirects them to index page after setting session and cookie
echo "<script>location.href='../manage/'</script>";
Every page requiring username and pass:
I then have every page which requires a username include the following code at the top of the file:
session_start();
// A function to check whether a value is md5 (kinda)
function CheckMd5($check){
$valid = preg_match('/^[a-z0-9]{32}$/',$check);
if($valid != 1){
setcookie("uid", "", time()-3600);
setcookie("pss", "", time()-3600);
session_start();
session_destroy();
echo("<script>location.href='login.php?ref=edit'</script>");
}
}
// connect.php connects to MySQL database
require("connect.php");
$access = 0;
if(isset($_COOKIE['uid'])){
$username = $_COOKIE['uid'];
CheckMd5($username);
$pass = $_COOKIE['pss'];
CheckMd5($pass);
$check = mysql_query("SELECT * FROM users WHERE username_enc = '$username'") or die (mysql_error());
while($info = mysql_fetch_array($check)){
if($pass == $info['password']){
$access = 1;
}
$username = $info['username'];
}
}
if(isset($_SESSION['ses_user'])){
$username = $_SESSION['ses_user'];
CheckMd5($username);
$pass = $_SESSION['ses_pass'];
CheckMd5($pass);
$check = mysql_query("SELECT * FROM users WHERE username_enc = '$username'") or die (mysql_error());
while($info = mysql_fetch_array($check)){
if($pass == $info['password']){
$access = 1;
}
$username = $info['username'];
}
}
if($access != 1){
echo "<script>location.href='login.php?ref=edit&id=".$_GET['id']."'</script>";
}else{
// Page Content Goes Here
}
Any tips or reassurance would be appreciated. :icon_eek: