douzhang5984 2019-07-24 12:53
浏览 86
已采纳

当我知道它是正确的时,PHP脚本会说“无效密码”

I'm building an intentionally flawed application for presentation purposes for work. It's left vulnerable to demonstrate improper practices and possibly encourage those who would exploit it to do so. There is ample logging on the server so we're able to see when people poke around. I just need a simple login page that uses a PHP script to connect to the SQL database. I can verify connectivity through the SQL server logs, however, the script returns and says "invalid password" when I know the password and username I'm inputting are correct.

I have tried swapping bits of code from other resources / what I know and I get internal error 500

// POST variables
$user=$_POST['user'];
$pass=$_POST['pass'];

// MD5 hash
//$pass=md5($pass);



$sql = "SELECT * FROM login WHERE user = '$user' AND pass = '$pass'";

// Query Login 
$stmt = sqlsrv_query($conn, $sql);
if( $stmt === false ) {
     die( print_r( sqlsrv_errors(), true));
}

// Login validation
if(sqlsrv_has_rows($stmt)) {
header('Location: landing.html');
}
else {
echo "Wrong username or password!";
}

I expect the connection to work, instead, it just throws up my "Wrong username or password!" statement

展开全部

  • 写回答

1条回答 默认 最新

  • dongxun1978 2019-07-24 14:47
    关注

    The way that your scripts is written is incorrect. To check for a correct username and password, I wouldn't check if there are rows. I understand this isn't real and it's meant to show bad scripting/coding/etc however, this is really bad. The reason why it keeps giving you "Wrong username or password!" is because there are no rows being returned. Try rewriting your script like this:

    <?php
    
    // Connect to Database Server
    $server = "serverName\sqlexpress";
    $connectionInfo = array("Database" => "dbName", "UID" => "username", "PWD" => "password");
    $conn = sqlsrv_connect($server, $connectionInfo);
    
    // POST variables
    $user = $_POST['user'];
    $pass = $_POST['pass'];
    
    $query = 'SELECT * FROM login WHERE `user` = "' . $user . '" AND `pass` = "' . $pass . '"';
    
    // Query Login
    $statement = sqlsrv_query($conn, $query);
    
    if ($statement) {
        $rows = sqlsrv_has_rows($statement);
        if ($rows === true && $rows >= 1) {
            header('Location: landing.html');
        } else {
            echo "Wrong username or password!";
        }
    } else {
        die(print_r(sqlsrv_errors(), true));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部