dtx6087 2017-09-26 03:24
浏览 30

无法使用PHP登录系统

May I know why it not redirect to the page that I've been looking for and I have some problem where if I enter the correct username its still show that I enter the wrong username/password. Here the snippet of my code. Anyone can help me? Thank you. And anyone how to make a session?

 <?php
    require('db.php');
    session_start();
    // If form submitted, insert values into the database.
    if (isset($_POST['username'])){

        $username = stripslashes($_REQUEST['username']); // removes backslashes
        $username = mysqli_real_escape_string($con,$username); //escapes special characters in a string
        $password = stripslashes($_REQUEST['password']);
        $password = mysqli_real_escape_string($con,$password);

    //Checking is user existing in the database or not
        $query = "SELECT * FROM `user` WHERE username='$username' and password='".md5($password)."'";
        $result = mysqli_query($con,$query) or die(mysqli_error());
        $rows = mysqli_num_rows($result);
        if($rows==1){
            $_SESSION['username'] = $username;
            header("Location: home.php"); // Redirect user to homeA.php
            }else{
                echo "<center><div class='form'><br><br><h1>Username/password is incorrect.</h1><br/>Click here to <a href='main.php'>Try Again</a></div></center>";
                }
                }
    else{
?>
  • 写回答

1条回答 默认 最新

  • dongwen3410 2017-09-26 06:17
    关注

    To start of with, please use prepared statements instead of string concatenation to construct sql statements. An easy solution to this problem would be to use PDO. Second, never ever hash passwords using MD5. Password hashing in PHP is done using the password_hash() and password_verify() functions. Below is an example of how a login page could work using previously mentioned methods.

    // Create a new PDO object using host (might be 127.0.0.1), dbname (your database name), user (your database username) and pass (your database password)
    $dbh = new PDO('mysql:host=localhost;dbname=test', $user, $pass);
    
    if (isset($_POST['username'])) {
    
        $stmt = $dbo->prepare('SELECT * FROM user WHERE username = :username');
        $stmt->bindParam(':username', $_POST['username']);
    
        if ($stmt->execute()) {
            $row = $stmt->fetch();
    
            if (password_verify($_POST['password'], $row['password'])) {
                // User password matched. Log them in
            } else {
                // User password didn't match
            }
        } else {
            // There was a problem executing query
        }
    
    }
    

    You will also need to hash your password using password_hash() when creating your users.

    password_hash($password, PASSWORD_DEFAULT)
    
    评论

报告相同问题?

悬赏问题

  • ¥15 在获取boss直聘的聊天的时候只能获取到前40条聊天数据
  • ¥20 关于URL获取的参数,无法执行二选一查询
  • ¥15 液位控制,当液位超过高限时常开触点59闭合,直到液位低于低限时,断开
  • ¥15 marlin编译错误,如何解决?
  • ¥15 有偿四位数,节约算法和扫描算法
  • ¥15 VUE项目怎么运行,系统打不开
  • ¥50 pointpillars等目标检测算法怎么融合注意力机制
  • ¥20 Vs code Mac系统 PHP Debug调试环境配置
  • ¥60 大一项目课,微信小程序
  • ¥15 求视频摘要youtube和ovp数据集