dongshen6060 2013-06-07 13:01
浏览 19
已采纳

使用伪数据从数据库中选择产生大于零的计数

im trying to create a login script and I feel like im almost there but for some reason when I test it by typing in incorrect usernames and passwords it still thinks it has got something from the database.

here is my form code:

        <form method="post" action="checklogin.php">
            <table>
                <tr>
                    <td><label style="color:#6b6a6b;" for="username">Username: </label></td>
                    <td><input class="textbox" type="text" name="username"></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td></td>
                </tr>
                <tr>
                    <td><label for="password">Password: </label></td>
                    <td><input class="textbox" type="password" name="password"></td>
                </tr>
                <tr>
                    <td>&nbsp;</td>
                    <td></td>
                </tr>
                <tr>
                    <td></td>
                    <td><input type="submit" name="login" id="login" value=""></td>
                </tr>
            </table>
        </form>

and here is my PHP code:

session_start();
$con=mysqli_connect("********", "*******", "******", "******");
$myroot = "";
$previousURL = parse_url($_SERVER['HTTP_REFERER'],PHP_URL_PATH);

if(isset($_POST['login'])){
    if(isset($_POST['username']) && isset($_POST['password']) && $_POST['username'] != "" && $_POST['password'] != ""){
        if (strlen($_POST['username']) > 20 || strlen($_POST['username']) < 4)
        {
            $error = 'Incorrect Length for Username or Password';
        }
        /*** check the password is the correct length ***/
        elseif (strlen($_POST['password']) > 20 || strlen($_POST['password']) < 4)
        {
            $error = 'Incorrect Length for Username or Password';
        }
        /*** check the username has only alpha numeric characters ***/
        elseif (ctype_alnum($_POST['username']) != true)
        {
            /*** if there is no match ***/
            $error = "Username must be alpha numeric";
        }
        /*** check the password has only alpha numeric characters ***/
        elseif (ctype_alnum($_POST['password']) != true)
        {
            /*** if there is no match ***/
            $error = "Password must be alpha numeric";
        }

        $username = $_POST['username'];
        $password = $_POST['password'];
        $username = stripslashes($username);
        $password = stripslashes($password);
        $username = mysqli_real_escape_string($con,$username);
        $password = mysqli_real_escape_string($con,$password);
        $result=mysqli_query($con,"SELECT * FROM RAE_customers WHERE username='" . $username . "' AND password='" . $password . "'")  or die(mysqli_error($con));

        $count=count($result);

        if($count==1){

            // Register $username
            $_SESSION['username'] = $username;
            var_dump($_SESSION['username']);
            echo "<br>";
            echo $count;
            /*if($previousURL == "/checkout.php"){
                header('Location: details.php');
            }
            elseif($previousURL == "/login.php"){
                header('location: index.php');
            }*/

        }
        else {
            $error = "Wrong Username or Password.";
        }
        if(isset($error)){
            echo $error;
        }
        else{
            echo "success";
        }
    }
    else{
        $error = "Please enter a Username and Password.";
    }

}
else{
    header('location:index.php');
}

When I type in dummy usernames and passwords the It treats it like they are correct.

I get these results

string(6) "dffddf" 1success

does anybody see the problem with my code?

  • 写回答

2条回答 默认 最新

  • dosryjij88555 2013-06-07 13:07
    关注

    $result is a mysqli result object so count($result) will always give 1 (unless the query fails).

    Try using:

    $result->num_rows;
    

    instead.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 数学建模,尽量用matlab回答,论文格式
  • ¥15 昨天挂载了一下u盘,然后拔了
  • ¥30 win from 窗口最大最小化,控件放大缩小,闪烁问题
  • ¥20 易康econgnition精度验证
  • ¥15 msix packaging tool打包问题
  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能