PHP:mysql设置登录

时间:2017-09-05 13:08:10

标签: php mysql database mysqli

我非常困惑, 我的代码输出:

  

发布登录:登录

     

发布密码:传递

     

数据库登录:登录

     

数据库传递:传递

     

数据库ID:1

     

数据库用户:IDKMyName

     

数据库创建者:True

     

数据库管理员:真实

     

数据库主人:真

     

失败

主要部分是最后一行"失败",它应该说登录go。发布的用户和数据库用户是相同的,发布的传递是相同的,所以idk。

PS。回声只是用于调试而不是最终代码。

 <?php

session_start();

$db_login = "";
$db_pass = "";
$db_id = "";
$db_user = "";
$db_creator = "";
$db_admin = "";
$db_master = "";

$servername = "localhost";
$username = "root";
$password = "";
$database = "main_db";

// Create connection
$conn = new mysqli($servername, $username, $password, $database);

$submitlogin = $_POST['user'];
$submitpass = $_POST['password'];

$query = $conn->query("SELECT * FROM main_table WHERE login = '$submitlogin' && pass = '$submitpass'", MYSQLI_USE_RESULT);

if ($query) {
   while ($row = $query->fetch_array()) {
       $db_login = $row['login'] . PHP_EOL;
       $db_pass = $row['pass'] . PHP_EOL;
       $db_id = $row['ID'] . PHP_EOL;
       $db_user = $row['user'] . PHP_EOL;
       $db_creator = $row['creator'] . PHP_EOL;
       $db_admin = $row['admin'] . PHP_EOL;
       $db_master = $row['master'] . PHP_EOL;
   }
}

echo "posted login: " . $submitlogin . "<br>";
echo "posted password: " . $submitpass . "<br>";
echo "database login: " . $db_login . "<br>";
echo "database pass: " . $db_pass . "<br>";
echo "database id: " . $db_id . "<br>";
echo "database user: " . $db_user . "<br>";
echo "database creator: " . $db_creator . "<br>";
echo "database admin: " . $db_admin . "<br>";
echo "database master: " . $db_master . "<br>";

if ($submitlogin != $db_login && $submitpass != $db_pass) {

    $_SESSION['ID'] = 'NULL';
    $_SESSION['loggedin'] = 'False';
    $_SESSION['login'] = '';
    $_SESSION['pass'] = '';
    $_SESSION['user'] = '';
    $_SESSION['creater'] = 'False';
    $_SESSION['admin'] = 'False';
    $_SESSION['master'] = 'False';


    echo"failed";
    echo"<a href = '/wip/login/>try again</a>";


}

else {

    $_SESSION['login'] = $db_login;
    $_SESSION['pass'] = $db_pass;
    $_SESSION['id'] = $db_id;
    $_SESSION['user'] = $db_user;
    $_SESSION['creator'] = $db_creator;
    $_SESSION['admin'] = $db_admin;
    $_SESSION['master'] = $db_master;
    $_SESSION['loggedin'] = 'True';

    echo "logged in";
    echo "<a href='/wip/>go</a>";

}

mysqli_close($conn);

?>

1 个答案:

答案 0 :(得分:4)

您正在为数据库中的数据添加换行符:

$db_login = $row['login'] . PHP_EOL; //<--here

所以你要比较:

"pass" == "pass\n"

如评论中所述,您还有其他一些问题,但这是您遇到问题的根本原因