【问题标题】:Sanitize Form Data清理表单数据
【发布时间】:2012-09-10 14:49:55
【问题描述】:

在使用 HTML/CSS 构建网站后,我只是在学习 PHP 和脚本的基础知识。我正在尝试制作一个提交到数据库的简单表单,只是为了收集电子邮件地址和姓名。以下是我正在使用的基本代码。我对它进行了一些调整,但没什么大不了的。我知道我需要清理这些数据,并且我已经阅读了数十篇关于如何执行此操作的帖子、文章等,我只是不知道如何向其中添加 escape_string 或 PHP 函数。我以为我做到了,我已经尝试了几十种方法,但是当我测试时,它们似乎没有任何区别。我知道这只是我的新手无知,但我有点扯头发,所以任何帮助都会很棒。

    <?php
    $con = mysql_connect("localhost","peter","abc123");
    if (!$con)
   {
   die('Could not connect: ' . mysql_error());
   }

    mysql_select_db("my_db", $con);

    $sql="INSERT INTO Persons (FirstName, LastName, Age)
    VALUES
    ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

    if (!mysql_query($sql,$con))
    {
    die('Error: ' . mysql_error());
    }
    echo "1 record added";

    mysql_close($con);
    ?> 

@rwhite35 最终结果应该是这样的吗?

<?php
// sanitize a string in prep for passing a single argument to system() (or similar)
function sanitize_system_string($string, $min='', $max='')
{
  $pattern = '/(;|\||`|>|<|&|^|"|'."\n|\r|'".'|{|}|[|]|\)|\()/i'; 
  // no piping, passing possible environment variables ($),
  // seperate commands, nested execution, file redirection, 
  // background processing, special commands (backspace, etc.), quotes
  // newlines, or some other special characters
 $string = preg_replace($pattern, '', $string);
 //make sure this is only interpreted as ONE argument
 $string = '"'.preg_replace('/\$/', '\\\$', $string).'"'; 
 $len = strlen($string);
  if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max)))
    return FALSE;
    return $string;
  }

$con = mysql_connect("localhost","root","root");
if (!$con)
  {
  die('Could not connect: ' . mysql_error());
  }

mysql_select_db("form-try", $con);

$firstname = sanitize_system_string($_POST['firstname'],2,44);
$lastname = sanitize_system_string($_POST['lastname'],2,44);

$sql="INSERT INTO Persons (FirstName, LastName, Age)
VALUES
('$_POST[firstname]','$_POST[lastname]','$_POST[age]')";

if (!mysql_query($sql,$con))
  {
  die('Error: ' . mysql_error());
  }
echo "1 record added";

mysql_close($con);
?>

【问题讨论】:

  • 如果您正在学习 PHP,请先使用 mysqliPDO 进行数据库使用
  • 还要查看准备好的语句,使用准备好的语句将减少对您的脚本/代码进行 SQL 注入攻击的机会。

标签: php mysql sql input-sanitization


【解决方案1】:

您的代码容易出现 SQL Injection。使用 PDOMYSQLI

使用 PDO 扩展的示例:

<?php

    $stmt = $dbh->prepare("INSERT INTO Persons (FirstName, LastName, Age) VALUES (?,?,?)");
    $stmt->bindParam(1, $_POST[firstname]);
    $stmt->bindParam(2, $_POST[lastname]);
    $stmt->bindParam(3, $_POST[age]);

    $stmt->execute();

?>

这将允许您使用单引号插入记录。

【讨论】:

  • 如果您想学习并知道自己在做什么,请不要使用 PDO。使用 mysql 和 mysqli 函数来了解这些密封对象内部正在播放的内容。然后,当您开始使用实际应用程序时,请使用 PDO。
  • 你的意思是mysql_*?已经按照说明书折旧了。
  • 让我看看我是否理解正确我会使用上面的 PDO 示例代替我正在使用的这一部分?'$sql="INSERT INTO Persons (FirstName , 姓, 年龄) VALUES ('$_POST[firstname]','$_POST[lastname]','$_POST[age]')"; if (!mysql_query($sql,$con)) { die('Error: ' .mysql_error()); } echo "添加了 1 条记录";
  • 插入的值应该被参数化。 PDO 会自动为您清理这些值。
  • 好吧,我显然有很多东西要学。我不确定我是否理解您所说的“输入的值应该参数化”是什么意思,但我感谢您尝试提供帮助并至少为我指明了一个新的学习方向。
【解决方案2】:

我认为 John Woo 是指您将 $_POST[firstname] 直接传递给您的 INSERT 语句。那很危险。这是我用来清理输入的简单函数,然后我将使用 mysqli 进行查询。这是一种“腰带和吊带”的方法。

// sanitize a string in prep for passing a single argument to system() (or similar)
function sanitize_system_string($string, $min='', $max='')
{
  $pattern = '/(;|\||`|>|<|&|^|"|'."\n|\r|'".'|{|}|[|]|\)|\()/i'; 
  // no piping, passing possible environment variables ($),
  // seperate commands, nested execution, file redirection, 
  // background processing, special commands (backspace, etc.), quotes
  // newlines, or some other special characters
 $string = preg_replace($pattern, '', $string);
 //make sure this is only interpreted as ONE argument
 $string = '"'.preg_replace('/\$/', '\\\$', $string).'"'; 
 $len = strlen($string);
  if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max)))
    return FALSE;
    return $string;
  }

然后在处理表单数据的代码中...

$firstname = sanitize_system_string($_POST['firstname'],2,44);
$lastname = sanitize_system_string($_POST['lastname'],2,44);

此过程将使用函数 sanitize_system_string 的结果实例化变量 $firstname 和 $lastname;正在删除诸如“{,

【讨论】:

  • 请看上面:) 它仍然将信息提交给数据库,但我不确定我是否会导致你的函数运行。我不确定如何测试它。
  • 您可能已经尝试过了,但是在您的表单字段中,输入 'string then {
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2020-12-14
  • 2010-09-23
  • 1970-01-01
  • 2019-04-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多