Login (PHP-SQL)
Publicado por GDP (9 intervenciones) el 22/04/2014 15:45:19
Buenas!
Tengo un problemilla con este codigo. La funcion se llama bien porque hace echo 10 pero de alli en adelante nada, Y necesito sacar correctamente esta funcion para poder proseguir :S.
La cosa es que no tengo ni idea de que es lo que falla, a ver si alguno de vosotros puedo encontrarlo.
Graias de antemano!
Tengo un problemilla con este codigo. La funcion se llama bien porque hace echo 10 pero de alli en adelante nada, Y necesito sacar correctamente esta funcion para poder proseguir :S.
La cosa es que no tengo ni idea de que es lo que falla, a ver si alguno de vosotros puedo encontrarlo.
Graias de antemano!
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
function login($email, $password, $mysqli) {
echo "10";
// Using prepared statements means that SQL injection is not possible.
if ($stmt = $mysqli->prepare("SELECT id, username, password, salt
FROM members
WHERE email = ? LIMIT 1")) {
echo "11";
$stmt->bind_param('s', $email); // Bind "$email" to parameter.
$stmt->execute(); // Execute the prepared query.
$stmt->store_result();
// get variables from result.
$stmt->bind_result($user_id, $username, $db_password, $salt);
$stmt->fetch();
// hash the password with the unique salt.
$password = hash('sha512', $password . $salt);
if ($stmt->num_rows == 1) {
// If the user exists we check if the account is locked
// from too many login attempts
if (checkbrute($user_id, $mysqli) == true) {
// Account is locked
// Send an email to user saying their account is locked
return false;
} else {
// Check if the password in the database matches
// the password the user submitted.
if ($db_password == $password) {
// Password is correct!
// Get the user-agent string of the user.
$user_browser = $_SERVER['HTTP_USER_AGENT'];
// XSS protection as we might print this value
$user_id = preg_replace("/[^0-9]+/", "", $user_id);
$_SESSION['user_id'] = $user_id;
// XSS protection as we might print this value
$username = preg_replace("/[^a-zA-Z0-9_\-]+/", "", $username);
$_SESSION['username'] = $username;
$_SESSION['login_string'] = hash('sha512', $password . $user_browser);
// Login successful.
return true;
} else {
// Password is not correct
// We record this attempt in the database
$now = time();
if (!$mysqli->query("INSERT INTO login_attempts(user_id, time)
VALUES ('$user_id', '$now')")) {
header("Location: ../phpSecure/error.php?err=Database error: login_attempts");
exit();
}
return false;
}
}
} else {
// No user exists.
return false;
}
} else {
echo "12";
// Could not create a prepared statement
header("Location: ../phpSecure/error.php?err=Database error: cannot prepare statement");
exit();
}
}
Valora esta pregunta


0