Here is a login system I am working on. I just want an opinion on it and whether I am going in the right direction or am just completely missing something.
The main thing I would like to know is: am I initializing my user session IDs correctly? Basically once the user enters their credentials, the system goes into the database to see if there is a match. If there is, then it adds the session_id
variable if it's not already set.
Now if it is set, then it checks your value against the one in the database. If they match, then you are entered into the system and then the session_id
s are changed on both sides (server/client), otherwise it returns false. Is this a correct or incorrect method?
function userIsLoggedIn()
{
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (isset($_POST['action']) and $_POST['action'] == 'login') {
if (!isset($_POST['user']) or $_POST['user'] == '' or !isset($_POST['password']) or $_POST['password'] == '') {
return FALSE;
}
array_push($_SESSION['mintime'], time());
array_shift($_SESSION['mintime']);
// minimum time (in seconds) between valid form submissions
if ($_SESSION['mintime'][1] - $_SESSION['mintime'][0] < 2) {
echo "to quick";
return FALSE;
} else {
$user = test_input($_POST['user']);
if ($GLOBALS['adminPage'] == 1 and $user != 'admin') {
return FALSE;
}
$passtemp = test_input($_POST['password']);
$password = hash('sha512', $passtemp . $user);
if ($user == 'admin') {
$GLOBALS["isAdmin"] = TRUE;
}
if (databaseContainsUser($user, $password)) {
if (!isset($_SESSION['session_id'])) {
setSessionID($user);
$_SESSION['loggedIn'] = TRUE;
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
return TRUE;
}
if ((isset($_SESSION['session_id'])) and (checkSessionID($user))) {
updateSessionID($user);
setSessionID($user);
$_SESSION['loggedIn'] = TRUE;
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
return TRUE;
} else {
unset($_SESSION['session_id']);
unset($_SESSION['loggedIn']);
unset($_SESSION['user']);
unset($_SESSION['password']);
return FALSE;
}
} else {
unset($_SESSION['loggedIn']);
unset($_SESSION['user']);
unset($_SESSION['password']);
return FALSE;
}
} //end else time check
} //end if login check
if (isset($_POST['action']) and $_POST['action'] == 'logout') {
unset($_SESSION['loggedIn']);
unset($_SESSION['user']);
unset($_SESSION['password']);
header('Location: ' . $_POST['goto']);
exit();
}
if (isset($_POST['action']) and $_POST['action'] == 'admin') {
header('Location: ' . $_POST['gotoadmin']); // gotoadmin = '..'
exit();
}
} //end if POST check
if (isset($_SESSION['loggedIn'])) {
return databaseContainsUser($_SESSION['user'], $_SESSION['password']);
}
} //end function
//validate input
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
return $data;
}
I store all passwords like this:
sha($password + salt, 512)
and the session IDs are made like this:
UUID()
$GLOBALS['adminPage'] == 1
is just plain wrong. \$\endgroup\$$GLOBALS["isAdmin"] = TRUE;
. This function also mixes incontroller code (decision to do POST, redirection,exit()
). Security problem is that is saves cleartext password into session. \$\endgroup\$UUID()
function. Have you checked it is designed to generate random and unpredictable strings with enough entropy? Else you have another security issue. \$\endgroup\$