PHP - Login (PHP-SQL)

 
Vista:

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!


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
Me gusta: Está pregunta es útil y esta claraNo me gusta: Está pregunta no esta clara o no es útil
0
Responder
Imágen de perfil de xve
Val: 3.943
Oro
Ha mantenido su posición en PHP (en relación al último mes)
Gráfica de PHP

Login (PHP-SQL)

Publicado por xve (6935 intervenciones) el 22/04/2014 16:40:18
Hola GDP, si se queda en la consulta sql, yo miraría el log del Apache... ya que me da la sensación de que no esta conectado a la base de datos, o no se ha seleccionado la base de datos correcta.

Al revisar el log del Apache (o el servidor web que utilices), veras donde tienes el problema.

Coméntanos, ok?
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Login (PHP-SQL)

Publicado por GDP (9 intervenciones) el 22/04/2014 16:59:48
Tienes razon, hay algun problema con la conexion, no se me habia ocurrido mirar ya que antes me saltaba el error de conexion del else.

1
2
3
4
[Tue Apr 22 15:55:00 2014] [error] [client 10.10.10.74] PHP Warning:  mysqli_connect(): (28000/1045): Access denied for user 'sec_user'@'localhost' (using password: YES) in /var/www/phpSecure/includes/db_connect.php on line 24
[Tue Apr 22 15:55:00 2014] [error] [client 10.10.10.74] PHP Notice:  Trying to get property of non-object in /var/www/phpSecure/includes/db_connect.php on line 25
[Tue Apr 22 15:48:01 2014] [error] [client 10.10.10.74] PHP Warning:  mysqli_connect(): (HY000/2005): Unknown MySQL server host 'HOST' (2) in /var/www/phpSecure/includes/db_connect.php on line 22
[Tue Apr 22 15:48:01 2014] [error] [client 10.10.10.74] PHP Notice:  Trying to get property of non-object in /var/www/phpSecure/includes/db_connect.php on line 23

No lo entinedo, la contrase;a en PHPMyAdmin y la del codigo :S

1
2
3
4
define("HOST", "localhost"); 			// The host you want to connect to. 
define("USER", "sec_user"); 			// The database username. 
define("PASSWORD", "4Fa98xkHVd2XmnfK"); 	// The database password. 
define("DATABASE", "sec_user");             // The database name.

1
2
3
4
5
include_once 'psl-config.php';
$mysqli = mysqli_connect(HOST, USER, PASSWORD, DATABASE);
if ($mysqli->connect_error) {
    header("Location: ../phpSecure/error.php?err=Unable to connect to MySQL");
    exit();
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar

Login (PHP-SQL)

Publicado por GDP (9 intervenciones) el 22/04/2014 17:16:50
Ok, esta ha sido mi solucion:

Quitar esos define y colocarlos como variables:

1
2
3
4
$HOST="localhost";
$USER="sec_user";
$PASSWORD="4Fa98xkHVd2XmnfK";
SDATABASE= "sec_user";


1
2
include_once 'psl-config.php'; $mysqli = mysqli_connect($HOST, $USER, $PASSWORD, $DATABASE);
if ($mysqli->connect_error) { header("Location: ../phpSecure/error.php?err=Unable to connect to MySQL"); exit();
Valora esta respuesta
Me gusta: Está respuesta es útil y esta claraNo me gusta: Está respuesta no esta clara o no es útil
0
Comentar