【问题标题】:the username is correct but password is wrong it doesn't redirect用户名正确但密码错误它不会重定向
【发布时间】:2020-11-03 03:54:45
【问题描述】:

我的 PHP 站点正在连接到 SQL Server 数据库,然后创建会话并重定向到“仪表板”:

  • 如果用户名和密码错误,它会重定向 - 效果很好。
  • 如果用户名存在但密码错误,则无法重新加载?

看起来很简单,但我遇到了麻烦,请帮忙。

还有让这段代码更好的建议也很好:)

<?php

session_start();

if ( ! empty( $_POST ) ) {

    if ( isset( $_POST['username'] ) && isset( $_POST['password'] ) ) {

        $username = $_POST['username']; 
        $password = $_POST['password'];

        $connectionInfo = array( "Database"=>"WebUIUsers", "UID"=>"DBUser", "PWD"=>"Password1234");
        $conn = sqlsrv_connect( "sqlserver01", $connectionInfo);

            if( $conn ) {
                // Connection established

                $sql = "SELECT * FROM tbl_webui_users WHERE username='$username'";
                $stmt = sqlsrv_query( $conn, $sql );

                if(!(sqlsrv_fetch_array( $stmt )) >=1){
                    header("Location: ./index.php");
                }

                // if username exists but password is wrong redirect to try again ?

                    while( $row = sqlsrv_fetch_array( $stmt ) ) {

                        if( $row[password] === $password ) {

                            $_SESSION['user_session'] = $username;
                            header("Location: ./dashboard.php");
                            sqlsrv_free_stmt( $stmt);
            
                        }else{
                            header("Location: ./index.php");
                        } //end if( $row[password] == $password )

                    } //end while( $row = sqlsrv_fetch_array( $stmt ) )

            }else{
                
                echo "Connection to database could not be established.";
                ( print_r( sqlsrv_errors(), true));

            } //end if( $conn )
 
    } //end if

} // end if

?>

【问题讨论】:

  • 此代码极易受到sql injection 的影响。你应该使用prepared statements with parameter binding
  • 感谢 Wesley,阅读了这篇文章,将进行一些编辑 :)
  • 另外,您不应该将密码以明文形式存储在数据库中,而应使用知名库(例如 bcrypt:stackoverflow.com/questions/4795385/…)对其进行哈希处理
  • 避免对数据库对象使用 Systems Hungarian Notation(即不要给表添加 tbl_ 之类的前缀 - 因为 webui_users 可能是 VIEW 或 UDF 而不是 BASE TABLE)。
  • 这种意外行为的实际原因是您调用了两次sqlsrv_fetch_array(),所以while ($row = sqlsrv_fetch_array($stmt)) { ... } 根本没有返回任何行。

标签: php html sql-server sqlsrv


【解决方案1】:

这种意外行为的实际原因是您调用了两次sqlsrv_fetch_array(),所以while ($row = sqlsrv_fetch_array($stmt)) { ... } 根本不会返回任何行。

但您至少需要考虑以下几点:

  • 始终在语句中使用参数以防止可能的 SQL 注入问题。正如documentation 中提到的... sqlsrv_query 函数既可以进行语句准备,也可以执行语句,并且可以用于执行参数化查询
  • 不要在数据库中以明文形式存储密码。

以下基于您的代码的基本示例是您问题的可能解决方案:

<?php
session_start();
if (!empty($_POST)) {

    if (isset($_POST['username']) && isset($_POST['password'])) {

        $username = $_POST['username']; 
        $password = $_POST['password'];
        
        $connectionInfo = array("Database"=>"WebUIUsers", "UID"=>"DBUser", "PWD"=>"Password1234");
        $conn = sqlsrv_connect("sqlserver01", $connectionInfo);
        if ($conn === false) {
            //echo "Connection to database could not be established: ".print_r(sqlsrv_errors(), true);
            header("Location: ./index.php");
            exit;
        }   

        $sql = "SELECT * FROM tbl_webui_users WHERE username = ?";
        $prms = array($username);
        $stmt = sqlsrv_query($conn, $sql, $prms);
        if ($stmt === false) {
            //echo "Error (sqlsrv_query): ".print_r(sqlsrv_errors(), true);
            header("Location: ./index.php");
            exit;
        }   
        
        // User doesn't exists
        if (!sqlsrv_has_rows($stmt)) {
            header("Location: ./index.php");
            exit;
        }   
        
        // User exists, but the password is wrong
        $row = sqlsrv_fetch_array($stmt));
        if ($row === false) {
            header("Location: ./index.php");
            exit;
        }   
        if ($row["password"] === $password) {
            $_SESSION['user_session'] = $username;
            header("Location: ./dashboard.php");
        } else {
            header("Location: ./index.php");
        }
    }

}

?>

【讨论】:

  • 谢谢@Zhorov,现在我必须处理我的 sql 数据库和安全密码存储 :)
猜你喜欢
  • 1970-01-01
  • 2017-10-25
  • 2015-05-15
  • 1970-01-01
  • 1970-01-01
  • 2018-11-20
  • 2021-10-08
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多