douqi0090 2017-09-04 08:56
浏览 65
已采纳

准备好的PDO语句在设置变量后中断代码

I have taken a login script from one of my other projects and was setting it up for my current one. After creating the database (it has no entrys) and uploading the files to the FTP, I went to the INDEX page but after I tried logging in once with incorrect details nothing happend, normally it should display a message saying 'incorrect credentials' but it didn't. I set up echo commands at various points within the code and managed to narrow down the error to one line. That line is setting a variable called $sql to a prepared SELECT statement, for some reason this is stopping the code as any echo commands placed after that will not run. Any ideas as to what is going on and how I can fix it? Or do you guys think it would be better to follow a more up login system? That seems to include tokens which will also be helpful for security.

        // Confirm login
            echo "1";
            include("config.php");
            $username = $_POST['username'];
            $password = $_POST['pass'];
            if(isset($_POST) && $username != '' && $password != ''){
                echo "2".$username;
                $sql = $dbh->prepare("SELECT id,password,psalt FROM users WHERE username=?");
                echo "3";
                $sql->execute(array($username));
                if($sql->rowCount() > 0) {
                while($result = $sql -> fetch()){
                    $pass = $result['password'];
                    $p_salt = $result['psalt'];
                    $id = $result['id'];
                }
                echo "3";
                $site_salt="salthashhere";
                $salted_hash = hash('sha256',$password.$site_salt.$p_salt);
                if($pass == $salted_hash){
                    echo "5";
                    $_SESSION['user'] = $id;
                    header("Location:home.php");
                } else {
                     echo "<h2>Username/Password is Incorrect.</h2>";
                     echo "<a href='register.php'>Register Here</a>";
                }
            }
  • 写回答

1条回答 默认 最新

  • douao3063 2017-09-04 09:51
    关注

    One thing you need to note rowCount() is not guaranteed to give accurate results when used within the SELECT statement, its normally used to returns the number of rows affected by the last DELETE, INSERT, or UPDATE statement executed by the corresponding PDOStatement object.

    After select in your case you might need to fetch all your results as an array then use the count() method to count the number of elements returned.

    so try the following code :

    <?php
    // Confirm login
    echo "1";
    include("config.php");
    $username = $_POST['username'];
    $password = $_POST['pass'];
    if (isset($_POST) && $username != '' && $password != '') {
        echo "2" . $username;
        $sql = $dbh->prepare("SELECT id,password,psalt FROM users WHERE username=?");
        echo "3";
        $sql->execute(array($username));
        $result = $sql->fetchall(); //fetch all results as array
        if (count($result) > 0) {
            foreach ($result as $key => $row) {
                $pass   = $row['password'];
                $p_salt = $row['psalt'];
                $id     = $row['id'];
    
                echo "3";
                $site_salt   = "salthashhere";
                $salted_hash = hash('sha256', $password . $site_salt . $p_salt);
                if ($pass == $salted_hash) {
                    echo "5";
                    $_SESSION['user'] = $id;
                    header("Location:home.php");
                } else {
                    echo "<h2>Username/Password is Incorrect.</h2>";
                    echo "<a href='register.php'>Register Here</a>";
                }
            }//end foreach
    
        } else {
            // account does not exist
            echo "<h2>account does not exist.</h2>";
            echo "<a href='register.php'>Register Here</a>";
        }
    }
    ?>
    

    PS : You might wanna look and take the advantage of php builtin password_hash() and password_verify() they are much more secured.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥200 csgo2的viewmatrix值是否还有别的获取方式
  • ¥15 Stable Diffusion,用Ebsynth utility在视频选帧图重绘,第一步报错,蒙版和帧图没法生成,怎么处理啊
  • ¥15 请把下列每一行代码完整地读懂并注释出来
  • ¥15 pycharm运行main文件,显示没有conda环境
  • ¥15 寻找公式识别开发,自动识别整页文档、图像公式的软件
  • ¥15 为什么eclipse不能再下载了?
  • ¥15 编辑cmake lists 明明写了project项目名,但是还是报错怎么回事
  • ¥15 关于#计算机视觉#的问题:求一份高质量桥梁多病害数据集
  • ¥15 特定网页无法访问,已排除网页问题
  • ¥50 如何将脑的图像投影到颅骨上