PDO odbc returns bad hashed password string

时间:2016-04-04 18:18:48

标签: php sql-server-2008-r2 php-password-hash pdo-odbc

Im hashing my users password into a SQL Server 2008 R2. The registration goes fine. The hashed password is saved correctly through this code:

$password = password_hash($password, PASSWORD_DEFAULT);
    // Insert the new user into the database 
    $insert_stmt = $db->conn->prepare("INSERT INTO $db->DB_NAME.dbo.SGU_USUARIO (USUARIO, EMAIL, CONTRASENA, RUT_ALUMNO, ID_PERFIL)"
            . " VALUES (:USUARIO, :EMAIL, :CONTRASENA, :RUT_ALUMNO, :ID_PERFIL)");

    $insert_stmt->bindParam(':USUARIO', $usuario, PDO::PARAM_STR);
    $insert_stmt->bindParam(':EMAIL', $email, PDO::PARAM_STR);
    $insert_stmt->bindParam(':CONTRASENA', $password, PDO::PARAM_STR);
    $insert_stmt->bindParam(':RUT_ALUMNO', $rut_alum, PDO::PARAM_STR);
    $insert_stmt->bindParam(':ID_PERFIL', $id_perfil, PDO::PARAM_INT);

    if (! $insert_stmt->execute()) {
        $error_msg = '<div class="alert alert-danger"><strong>Error!</strong> Registro de usuario fallido.</div>';
    }else{
        $error_msg = '<div class="alert alert-success">Usuario registrado exitosamente.</div>';
    }

When i want to do the login script i do the following:

if ($stmt = $db->conn->prepare("SELECT * FROM $db->DB_NAME.dbo.SGU_USUARIO WHERE EMAIL = :EMAIL")) {

        $stmt->bindParam(':EMAIL', $email, PDO::PARAM_STR);
        $stmt->execute();
        $resultado = $stmt->fetch(PDO::FETCH_ASSOC);

        if(!empty($resultado)){

            if (password_verify($password, $resultado['CONTRASENA'])) {
                    // 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
                    $resultado['USUARIO'] = preg_replace("/[^a-zA-Z0-9_\-]+/", 
                                                                "", 
                                                                $resultado['USUARIO']);
                    $_SESSION['username'] = $resultado['USUARIO'];
                    $_SESSION['login_string'] = hash('sha512', 
                              $resultado['CONTRASENA'] . $user_browser);
                    // Login successful.
                    return true;
                }
}

This always returns false because for some reason the users info is arriving with problem in the email and the hashed password.

If i do var_dump() from the users info that i get with the first query i get this:

array (size=6)
  'ID_USUARIO' => string '1' (length=1)
  'USUARIO' => string 'joshe.onate' (length=11)
  'EMAIL' => string '�|����� |�����!���' (length=20)
  'CONTRASENA' => string '�|�����`|�����!�������!   �������RUT_ALUMNO�����!�������!���' (length=60)
  'RUT_ALUMNO' => string '166604168' (length=9)
  'ID_PERFIL' => string '2' (length=1)
|� |�!
|�`|�!!RUT_ALUMNO!!

For some reason the hashed and the email fields are arriving with problems.

Thank you in advanced for you help.

1 个答案:

答案 0 :(得分:0)

Ok so my problem was that in SQl Server i said that those fields were MAX varchar. I leave it in varchar(100) and everything returned correctly.