【问题标题】:Why does my code always return "Wrong username and password"?为什么我的代码总是返回“错误的用户名和密码”?
【发布时间】:2014-04-18 15:21:42
【问题描述】:

对不起,如果这是一个常见的问题,但我很难过。

<?php
session_start();
unset($_SESSION['logerror']);

$host="localhost"; // Host name
$db_name="website_database.sqlite"; // Database name
$tbl_name="User"; // Table name

$con= sqlite_open("Website_Database.sqlite");

// username and password sent from form
$username=$_POST['Username'];
$password=$_POST['Password'];


$sql="SELECT * FROM ".$tbl_name." WHERE Username='".$username."' and Password='".$password."'";
$result= sqlite_query($con,$sql);

// Mysql_num_row is counting table row
$count= sqlite_num_rows($result);

// If result matched $username and $password, table row must be 1 row


if($count==1){

// Register username, $password and redirect to file "login_success.php"
session_register("username");
session_register("password");
echo "Login Successful";
}
else {
echo "does this work nikhil";
}
?>

我正在使用 SQLite,它通过 USB 运行。该文件是 website_database.sqlite

该表是用户。里面的数据是用户名:000001和密码:密码

上一个表单来的post数据是这样的

<form action="checklogin.php" method="post">
        <div class="wrap"> <!--make a container that will have all our elements and will be the main stucture of you pages-->
            <div class="logo"></div><!--The logo container-->
            <div class="header_text">Room Booking</div><!--The main header of the fomr-->
            <div class="logins_details_container"><!--The top container-->
                <table class="fields_container"> <!--A simple 2x2 table for the form. We choose to use table as for this simple kind of stuff helps us to be everything well stuctured and putting less attributes at the css-->
                    <tr>
                        <td><span>Username</span></td>
                        <td><input class="field" id="Username" name="Username" value="" placeholder="" type="text" /></td>
                    </tr>
                    <tr>
                        <td><span>Password</span></td>
                        <td><input  class="field" id="Password" type="Password" name="Password" value="" placeholder="" /></td>
                    </tr>
                </table>
                <div class="remeber_me_container"><!--A container for the Remember me text and the checkbox-->
                    <div class="remember_me">Remember Me?</div>
                    <input  id="remeber_me" name="" value="" type="checkbox" />
                </div>
                <div class="login_btn"><!-- The login button-->
                    <input name="login_btn" value="Login" type="submit" />
                </div> 
            </div>
    <form/><!-- End of the logins_details_container --> 

【问题讨论】:

  • 您对 SQL 注入攻击完全开放,如果您还没有被黑客入侵,那么您将被黑客入侵。请使用准备好的/参数化查询来防止这种情况发生。另见:How can I prevent SQL injection in PHP?
  • 简单:您的密码表单元素为空白name=""
  • 如有疑问,请始终print_r($_POST); 以查看用户名和密码是否得到POSTed。正如 Fred 上面提到的,您的密码名称 attribute 为空。如果你有enabled error reporting,你会很容易发现的。
  • @user3548794:不用担心 SQL 注入 - 伙计,不!不!
  • @user3548794:好吧,如果你决定成为一名汽车修理工,请提醒我永远不要带着不可靠的刹车把我的车带给你;)

标签: php sql forms sqlite


【解决方案1】:

在您的表单中,密码输入字段的名称属性为空。我想你会想要这个:

<td><input  class="field" id="Password" type="Password" name="Password" value="" placeholder="" /></td>

【讨论】:

  • 欢迎加入@user3548794
【解决方案2】:

我只是将其作为临时答案发布,因为评论太多了。

无论出于何种原因,您当前的代码似乎并未发布密码值,因此让我们尝试使用一些非常简单的代码来缩小范围。用这个替换checklogin.php 中的所有代码并启动它。输入用户并通过,点击提交,让我知道print_r 的结果是什么。

if ($_SERVER['REQUEST_METHOD'] != 'POST') {
    echo '<form action="checklogin.php" method="post">
    <input name="Username"><br>
    <input type="Password" name="Password"><br>
    <input value="Login" type="submit">
    </form>
    ';
}else{
    print_r($_POST);
}

如果它返回用户名和密码,请将print_r($_POST); 替换为您的其他 PHP 代码并查看登录是否有效。再次,发布结果...

我唯一能看到的是您的 HTML 中缺少 &lt;/div&gt; 和不正确的 &lt;/form&gt; 结束标记。你也打错了几次remember_me。

<form action="checklogin.php" method="post">
    <div class="wrap"> <!--make a container that will have all our elements and will be the main stucture of you pages-->
        <div class="logo"></div><!--The logo container-->
        <div class="header_text">Room Booking</div><!--The main header of the fomr-->
        <div class="logins_details_container"><!--The top container-->
            <table class="fields_container"> <!--A simple 2x2 table for the form. We choose to use table as for this simple kind of stuff helps us to be everything well stuctured and putting less attributes at the css-->
                <tr>
                    <td><span>Username</span></td>
                    <td><input class="field" id="Username" name="Username" value="" placeholder="" type="text" /></td>
                </tr>
                <tr>
                    <td><span>Password</span></td>
                    <td><input class="field" id="Password" type="Password" name="Password" value="" placeholder="" /></td>
                </tr>
            </table>
            <div class="remember_me_container"><!--A container for the Remember me text and the checkbox-->
                <div class="remember_me">Remember Me?</div>
                <input id="remember_me" name="" value="" type="checkbox" />
            </div>
            <div class="login_btn"><!-- The login button-->
                <input name="login_btn" value="Login" type="submit" />
            </div> 
        </div>
    </div><!-- THIS ONE!-->
</form><!-- NOT <form/>!--> 

【讨论】:

  • 是的,这个返回了所有 3 个值——用户名、密码和登录名。 .
  • 天哪,你的魔术师。你做了什么来完成这项工作!它返回了所有 3 个值并且登录成功
  • 我实际上什么都没做,你的 HTML 中的某个地方显然有错误,但我看不到它。
猜你喜欢
  • 2016-02-16
  • 2021-10-23
  • 2013-03-21
  • 2023-04-01
  • 1970-01-01
  • 2017-12-30
  • 1970-01-01
  • 2020-02-07
  • 1970-01-01
相关资源
最近更新 更多