Simple piece of PHP code:
#login.php
$_SESSION['valid_user_id'] = getUserId($username, $password);
#user_auth_fns.php
function getUserId($username, $password)
{
$username = addslashes($username);
$username = mysql_real_escape_string($username);
$password = addslashes($password);
$password = mysql_real_escape_string($password);
$conn = db_connect();
$result = $conn->query("select id from user where username='$username' and password= sha1('$password')");
if (!$result) {
throw new Exception('Could not retrieve your user id.');
}
if ($result->num_rows > 0) {
return $result;
} else {
throw new Exception('Could not retrieve your user id.');
}
}
"return $result" is wrong, however I have no idea what I should put there in order to return the id from a certain user. The PHP manual didn't provide the answer either. I know this function works because replacing
return $result by return "test"
returns the correct value as expected.
$username = addslashes($username);is not needed and does not add security anyway. Use$username = htmlentities($username), but only when getting the username out of the database, not when putting it into the database.and password= sha1('$password')");withand password = SHA2(CONCAT(salt,'$password'),512)");, SHA1 is broken and without a salt it will not withstand rainbow tables, allowing someone with one to break your passwords in seconds.addslashes()followed bymysql_real_escape_string()? It burns! The goggles do nothing!