繁体   English   中英

PHP登录:响应不佳

[英]PHP login: does not respond well

它是一个简单的用户登录。 我只是想了解这个概念。 让我解释一下我的代码问题。 这是我的表格:

<form action = "check_login.php" action = "POST">
    User:<input type="text" name="user"><br/>
    Password:<input type="password" name="password"><br/>
    <input type="submit" name="submit" value="Sign In">
</form>

check_login.php:

    <?php
    error_reporting(E_ALL);
    $username = $_POST['user'];
    $password = $_POST['password'];

    if($username && $password) {
        $host = 'localhost';    
        $user = 'root';

        $con = mysql_connect($host, $user, '') or die("Couldn't Connect");
        mysql_select_db('first_site');

        $sql = "SELECT * FROM user WHERE user_name= '$username' and user_password ='$password'";
        $query = mysql_query($sql, $con);

        $count = mysql_num_rows($query);
        echo $count;

     }
     else {
        die "Enter username and password";
     }

我维护了数据库first_site和table user 该表有3列:id,user_name和user_password。

如果我输入用户名和密码,则$count变量的值应为1,但恰好为0。我一无所知。 救命!!

该代码本身很危险,您还没有使其安全可以防止注入”

 <?php
error_reporting(E_ALL);
// Escaped For SQL Injection Prevention
$username = mysql_real_escape_string($_POST['user']);
$password = mysql_real_escape_string($_POST['password']);

if($username && $password) {
    $host = 'localhost';    
    $user = 'root';

    $con = mysql_connect($host, $user, '') or die("Couldn't Connect");
    mysql_select_db('first_site');

    $sql = "SELECT user_name FROM user WHERE user_name= '$username' and user_password ='$password'";
    $query = mysql_query($sql, $con);

    // If Exists
    if( $query ) {
        $count = mysql_num_rows($query);
         echo $count;
    }
    else {

        die(mysql_error());

    }

 }
 else {
    die ("Enter username and password");
 }

还要将表格更改为

<form action = "check_login.php" method = "POST">
User:<input type="text" name="user"><br/>
Password:<input type="password" name="password"><br/>
<input type="submit" name="submit" value="Sign In">

尝试上面的方法,它应该使我们的mysql错误(如果有)错误,它也比原始错误更安全。

解决问题的最佳方法是逐一尝试。 添加一些echo $ foo,以查看登录过程中的变量。

此外,可能要删除以下内容中的单引号:

$sql = "SELECT * FROM user WHERE user_name= '$username' and user_password ='$password'";

形式上有两个动作,您必须输入以下内容

<form action = "check_login.php" method= "POST">

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM