【问题标题】:PHP function for checking POST security and storing variable用于检查 POST 安全性和存储变量的 PHP 函数
【发布时间】:2013-03-04 07:50:05
【问题描述】:

我正在寻找有关如何制作功能以使其更容易的信息。我知道有一种更简单的方法,然后将我想要的发布变量写入 PHP。

if ($_SERVER['REQUEST_METHOD'] == 'POST') {
  $name =   isset($_POST['name']) ? htmlentities($_POST['name']) : '';
  $email =  isset($_POST['email']) ? htmlentities($_POST['email']) : '';
  $interest = isset($_POST['interest']) ? htmlentities($_POST['interest']) : '';
  $checkbox = isset($_POST['checkbox']) ? htmlentities($_POST['checkbox']) : '';

到目前为止,我想出了一个这样的函数:

function req_post($n){
  '$'$n = isset($_POST["$n"]) ? htmlentities($_POST["$n"]) : '';
}

我知道我做错了,对 PHP 来说有点新。任何帮助将不胜感激。提前谢谢你。

【问题讨论】:

  • 你真的不应该htmlentitiesing 你的输入。这只是输出的工作。
  • 你为什么要这样做?对于 SQL 注入?使用 PDO 并拥有美好的生活。
  • 我不明白:'$'$n =

标签: php html function post http-post


【解决方案1】:

制作这样的功能似乎很诱人,看似删除重复的代码等,但最终总是会咬你。

您的代码显示您转义了所有 POST 数据,为下一个环境做好准备,该环境将是一个 html 页面。

所以,如果你只是将 $email 输出到一个 html 页面,这似乎是值得的。

但是,如果您同时输出到网页“谢谢 $email”并将其存储到数据库,那么您还没有为数据库转义它,因此您面临 sql 注入攻击的风险。

在您了解得更清楚之前,您最好保留 $_POST['email'] 并在输出时转义它。

echo htmlentities($_POST['email']);

$query = 'update names set email = "'.mysql_real_escape_string($_POST['email']).'" where ID =1';

或者最好使用 PDO/Mysqli 和准备好的语句,它们会为您转义。

htmlentities 是 html 输出转义的方法 mysql_real_escape_string 是 mysql 数据库的一种转义方法(虽然已经过时,正如我和其他人所说的那样)。

事实是,如果你遇到像 $email 这样的 var,你会想,现在等一下,这个逃逸是否为下一个环境做好了准备?它是从哪里来的?

当您看到 $_POST['email'] 时,您就知道您正在处理可能非常肮脏和危险的数据,并且您要小心处理。

您最好花时间进行一些过滤,并确定如果 $_POST['email'](或名称或其他)确实为空,下一步该做什么——重新定位用户,显示警告给用户等等。

助记符 FIEO 提供了基本规则、过滤输入、转义输出,您可以通过花几个小时研究它来为自己省去很多未来的痛苦。

【讨论】:

  • 感谢您的详细解释。我实际上正在使用 PDO 为数据库发布此信息。我没有意识到我用错了 htmlentities!这是否意味着我可以使用它? $interest = isset($_POST['interest'])
  • 执行 var_dump($_POST) 并查看当该元素提交为空时发送的内容。如果这通过了您的“业务规则”,即进入我的数据库的空字符串是可以接受的,并且将兴趣留空是可以接受的,那么只需将 $_POST['interest'] 直接放入您准备好的语句中。但是,如果兴趣是从 4 个兴趣的下拉列表中选择的,那么您应该通过检查它是否确实是这 4 个允许值之一来过滤传入的兴趣。否则,业务规则会启动,您会中止、重新定位用户或类似的事情。
  • 非常有见地。再次感谢您!
【解决方案2】:
//If you intend to put into database and you need to use a function

function clean($value){
$array = array("name", "email", "interest", "checkbox"); //For added security
   if(isset($_POST[$value]) && in_array($value, $array)){ //Since you are only concerned with post;
    return mysqli_real_escape_string($db_connection, $_POST[$value]); //and YES, use mysqli - forget what is deprecated in future projects
 }
}

$clean_name = clean("name");
$clean_email = clean("email");
$clean_interest = clean("interest");
$clean_checkbox = clean("checkbox");

echo $clean_email;

【讨论】:

    【解决方案3】:
    function htmlentities_and_checkISset($string){
    
        $result = isset($string) ? htmlentities($string) : '';
    
        return $result
    }
    

    或者这样做

    function htmlentities_and_checkISset(){
    
    
            // get number of arguments in this function
             $numargs = func_num_args();
    
             // get arguments comming to the function
             $arg_list = func_get_args();
    
             // for each arg do the thing you want and then store it in an array
             for ($i = 0; $i < $numargs; $i++) {
    
             $data[] = isset($arg_list[$i]) ? htmlentities($arg_list[$i]) : '';
    
            }
    
         return $data;
        }
    

    你可以这样称呼它。

    $data = htmlentities_and_checkISset($name,$email,$interest,$checkbox);
    

    或者

     $data = htmlentities_and_checkISset($_POST['name'],$_POST['email'],$_POST['interest'],$_POST['checkbox']);
    

    【讨论】:

    • 检查isset 函数内部的变量是没有意义的,因为保证变量被设置在那里。尤其是当你遍历一个参数列表时,你不认为这些会被设置吗?
    • @deceze 我是为htmlentities做的,他可以在调用函数之前调用isset函数。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-12-31
    • 2011-12-03
    • 2012-02-22
    • 1970-01-01
    • 2012-12-16
    相关资源
    最近更新 更多