Greetings,
I'm looking for some advice on the below code.
I would like to know how dependable does the login code look and if anyone has anything that would improve it in a security stand-point..
I know nothing is 100% secure, but would like to know if anyone sees any flaws.
1) username,userid,password is fetched form the database, if no user is found give the user a userid of 0.
2) if user is not found proceed to let the user login.
3) if user is found then send them to index.php.
code:
error_reporting(E_ALL &~E_NOTICE);
require('./global.php');
// check if user already has a cookie
$fetchuserinfo = $data->query(
"SELECT userid
FROM admins
WHERE userid = '".$_COOKIE['gameuid']."'");
$userinfo = $data->fetch_array($fetchuserinfo);
if(mysql_num_rows($fetchuserinfo) == 0)
{
$userinfo['userid'] = 0;
}
if ($userinfo['userid'] != 0)
{
// if user is already logged in then send them to index.php
header('Location: index.php');
exit;
}
if ($_POST['submit'])
{
$validate = $data->query("SELECT userid,username,password
FROM `admins` WHERE `username` = '" .
addslashes(htmlspecialchars($_POST['username'])) . "'");
// check if username entered is valid
if (mysql_num_rows($validate) == 0)
{
echo 'Username does not exist';
exit;
}
else
{
$user = $data->fetch_array($validate);
// check if password matches
if(md5($_POST['password']) != $user['password'])
{
echo 'Sorry, your password is incorrect.';
exit;
}
else
{
setcookie('gameuser', $user['username'], time() + (3600 * 24 * 7));
setcookie('gameuid', $user['userid'], time() + (3600 * 24 * 7));
setcookie('gamepass', md5($user['password']), time() + (3600 * 24 * 7));
// user is now logged in and sent to index.php
header('Location: index.php');
exit;
}
}
}
else
{
// login form template code
eval("echo(\"" . template("loginpage") . "\");");
}