douzhang5984 2019-07-24 20: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 22: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));
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 易盾点选的cb参数怎么解啊
  • ¥15 MATLAB运行显示错误,如何解决?
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题