【问题标题】:The Defined function "mysql_entities_fix_string" in PHP isn't getting calledPHP中的定义函数“mysql_entities_fix_string”没有被调用
【发布时间】:2015-05-22 08:27:18
【问题描述】:
<?php
require_once 'login.php';
require_once 'welcome.php';
$db_server = mysql_connect($db_hostname,$db_username,$db_password);
if(!$db_server) die("Unable to connect with MySql : " . mysql_error());

mysql_select_db($db_database) or die("Unable to connect with db");


echo <<<_END
<form action = 'ps.php' method = 'post'><pre>
Enter your Username <input type = 'text' name = 'username'>
Enter your Password <input type = 'text' name = 'password'>
<input type = 'submit' value = 'Cl1ck M3'>
</pre></form>
_END;

if (isset($_POST['username']) && isset($_POST['password']))
{
    //echo "Fine till here1";
    echo $_POST['username']."  Without htmlentities <br>";
    $usernameP = mysql_entities_fix_string($_POST['username']);
    if (!$usernameP) die ("No value fetched in the variable usernameP");

    $passwordP = mysql_entities_fix_string($_POST['password']);
    if (!$passwordP) die ("No value fetched in the variable passwordP");

    $query = "SELECT * FROM hacker WHERE username = '$usernameP' AND password = '$passwordP'";
    $result = mysql_query($query,$db_server);
    if(!$result) die ("Unable to execute query : " . mysql_error());

    $rows = mysql_num_rows($result);

        $row = mysql_fetch_row($result);
        echo $row[0];
        if ($row[0] == '$username' && $row[1] == '$passwordP')
        {
            echo "Credentials Authorized";
        }

   function mysql_entities_fix_string($string)
         {
                return htmlentities(mysql_fix_string($string));
        }   

    function mysql_fix_string($string)
        {
                if (get_magic_quotes_gpc()) $string = stripslashes($string);
                return mysql_real_escape_string($string);
         }
    }   
mysql_close($db_server);

?>

我正在尝试测试一个简单的 PHP 页面。我正在使用函数“mysql_entities_fix_string”过滤掉恶意输入,但程序无法调用它。因此在 $usernameP 或 $passwordP 中没有获取任何值。 任何人都可以提出一些建议吗?

【问题讨论】:

  • 除了条件定义问题之外,这是一种错误的输入处理方法。 HTML 转义是一个模板输出问题,当您将 echo 内容转换为 HTML 标记时,您应该使用 htmlspecialchars 来完成此操作。 SQL 转义是一个查询准备问题,在将内容插入 SQL 字符串文字时应该这样做。 (或者,更好的是,通过在 mysqli 或 PDO 中使用参数化查询而不是已弃用的 mysql 函数来完全避免 SQL 转义的需要。)你不能在一个地方同时执行它们,你会弄错的内容不一致。

标签: php security php-5.5 secure-coding


【解决方案1】:

您正在有条件地定义函数。如果定义了函数,例如在if 语句中,它仅在您的代码对其执行之后 可用。相反,在主范围(“所有括号外”)中定义的函数是在文件的其余部分运行之前定义的。

【讨论】:

  • 我不清楚是否有人愿意回复。无论如何,我是 PHP 新手,所以要学习一些东西。明白你的意思了。赞许@Piskvor
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-05-08
  • 1970-01-01
  • 2014-02-16
  • 2015-06-21
相关资源
最近更新 更多