duanqiongdu9916 2018-10-15 11:51
浏览 33

使用MD5进行用户身份验证[关闭]

I'm building an app when i'm able to hash user passwords by using MD5. For the insertion it works correctly, but when i'm authenticating, it tells me the password is wrong

require_once('connect.php');
$data = json_decode(file_get_contents("php://input"));
$email = ($data->email);
$password = ($data->password);

$row = $conn->query("
    SELECT * 
    FROM user 
    WHERE email='".$email."' 
        AND password='".md5.$password."'
");
$row->setFetchMode(PDO::FETCH_ASSOC);

$userdetails = $row->fetchAll();
$user = $row->rowCount();

$error_message=array("message"=>("wrong"));

if ($user == 0) {
    echo json_encode($error_message);
} else {
    session_start();
    $_SESSION['user']=$userdetails;
    echo json_encode($userdetails);
}
  • 写回答

3条回答 默认 最新

  • doumi1099 2018-10-15 11:55
    关注

    Change

    $row = $conn->query("SELECT * from user where email='".$email."' and password='".md5.$password."'");
    

    to

    $row = $conn->query("SELECT * from user where email='".$email."' and password='".md5($password)."'");
    

    NB : You should use prepared statements and also don't use md5() use password_hash() and password_verify()

    Then in your registration page you will have.

    $hash = password_hash($password,password_default);  // store this hash
    

    Then your login page.

    $stmt = $conn->query("SELECT * from user where email= ? ");
    $stmt->execute(array($email));
    
    $row = $stmt->fetch();
    
    if(password_verify($password,$row['passwordFromDB'])){
    
        session_start();
        $_SESSION['user']=$userdetails;
        //return what needs to be returned
    }else{
        $error_message=array("message"=>("wrong"));
        echo json_encode($error_message);
    }
    

    NB : Make sure your database column have a length of 60+

    评论

报告相同问题?

悬赏问题

  • ¥15 DIFY API Endpoint 问题。
  • ¥20 sub地址DHCP问题
  • ¥15 delta降尺度计算的一些细节,有偿
  • ¥15 Arduino红外遥控代码有问题
  • ¥15 数值计算离散正交多项式
  • ¥30 数值计算均差系数编程
  • ¥15 redis-full-check比较 两个集群的数据出错
  • ¥15 Matlab编程问题
  • ¥15 训练的多模态特征融合模型准确度很低怎么办
  • ¥15 kylin启动报错log4j类冲突