提交空白值并返回 mysql 查询的问题

发布于 2024-10-22 18:36:45 字数 1113 浏览 3 评论 0原文

<?php
$link = mysql_connect('127.0.0.1', 'ilhan', 'password123');
if(!$link)
    {
        die('Could not connect: ' . mysql_error());
    }
mysql_select_db("metu", $link);
mysql_set_charset('utf8',$link);

if(isset($_POST["email"])) // AND strlen($_POST["email"])>1 solves the problem
//  but I didn't get why the page is redirected...
    {
        $email = mysql_real_escape_string($_POST["email"]);
        $password = mysql_real_escape_string($_POST["password"]);

        $result = mysql_query("SELECT password, id FROM users WHERE email = '$email'");

        if (!$result)
            {
                die('Invalid query: ' . mysql_error());
            }

        $row = mysql_fetch_assoc($result);

        if($row['password'] == $password) 
            {
                $_SESSION['valid_user'] = $row['id'];
                mysql_close($link);
                header("Location: http://www.example.com");
            }
        else $login = false;


        mysql_close($link);
    }
else $login = false;
?>

您认为如果用户提交空白电子邮件和空白密码,页面会被重定向吗?不应该,但页面已重定向。这是为什么?漏洞?

<?php
$link = mysql_connect('127.0.0.1', 'ilhan', 'password123');
if(!$link)
    {
        die('Could not connect: ' . mysql_error());
    }
mysql_select_db("metu", $link);
mysql_set_charset('utf8',$link);

if(isset($_POST["email"])) // AND strlen($_POST["email"])>1 solves the problem
//  but I didn't get why the page is redirected...
    {
        $email = mysql_real_escape_string($_POST["email"]);
        $password = mysql_real_escape_string($_POST["password"]);

        $result = mysql_query("SELECT password, id FROM users WHERE email = '$email'");

        if (!$result)
            {
                die('Invalid query: ' . mysql_error());
            }

        $row = mysql_fetch_assoc($result);

        if($row['password'] == $password) 
            {
                $_SESSION['valid_user'] = $row['id'];
                mysql_close($link);
                header("Location: http://www.example.com");
            }
        else $login = false;


        mysql_close($link);
    }
else $login = false;
?>

Do you think that the page will be redirected if the user submits a blank email and blank password? It shouldn't but the page is redirected. Why is that? Bug?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

來不及說愛妳 2024-10-29 18:36:45

我一开始就忽略了这个问题。
这里是:

if($row['password'] == $password) 

$row['password'] 等于空 $password。评估为 true

这就是我要做的

<?php
include 'db.php';    
if(isset($_POST["email"]))
{
    $sql = "SELECT id FROM users WHERE email='%s' AND password='%s'";
    $id  = dbGetOne($sql,$_POST["email"],MD5($_POST["email"].$_POST["password"]));
    if($id) 
    {
        $_SESSION['valid_user'] = $row['id'];
        header("Location: http://www.example.com");
        exit;
    }
}

I've overlooked the issue at first.
here it is:

if($row['password'] == $password) 

empty $row['password'] is equal to empty $password. evaluated to true

here is what I'd do it

<?php
include 'db.php';    
if(isset($_POST["email"]))
{
    $sql = "SELECT id FROM users WHERE email='%s' AND password='%s'";
    $id  = dbGetOne($sql,$_POST["email"],MD5($_POST["email"].$_POST["password"]));
    if($id) 
    {
        $_SESSION['valid_user'] = $row['id'];
        header("Location: http://www.example.com");
        exit;
    }
}
如歌彻婉言 2024-10-29 18:36:45

使用 mysql_num_rows 函数查找返回的行数。 mysql_query 出错时将返回 FALSE,但在您的情况下,如果电子邮件为空,您的 SQL 仍然是有效的 SQL。它只会返回一个空结果集。

Use mysql_num_rows function to find then umber of rows returned. mysql_query will return FALSE on error but in your case, your SQL is still a valid SQL if email is blank. It will just return an empty result set.

坏尐絯℡ 2024-10-29 18:36:45
  if(isset($_POST["email"])) 
 // AND strlen($_POST["email"])>1 solves the problem 
 // but I didn't get why the page is redirected...

如果 $_POST["email"] 是空字符串 -> isset 将返回 true

编辑:添加 if ( isset($_POST["email"]) && isset ($_POST["password"]) ) 以避免空密码并确保 <当 `if($row['password'] == $password)' 时,code>$row 不为空

  if(isset($_POST["email"])) 
 // AND strlen($_POST["email"])>1 solves the problem 
 // but I didn't get why the page is redirected...

if $_POST["email"] is empty string -> isset will return true

EDIT: add if ( isset($_POST["email"]) && isset ($_POST["password"]) ) to avoid empty passwords and make sure that $row is not empty when `if($row['password'] == $password)'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文