zoukankan      html  css  js  c++  java
  • PHP filter_input() 函数

       以往,对于常见的SQL注入等漏洞,采取的方式一般都是对数据进行过滤,而对$_GET/$_POST/$_COOKIE/$_SERVER等全局数组变量的直接使用是不够安全的,故PHP 5.2.0版本以后,推出Filter系列函数,对外部脚本的数据进行过滤,比如POST表单中的email邮箱进行验证,则将$filter参数设置FILTER_VALIDATE_EMAIL即可。

    函数名:filter_input

    作用:从脚本外部获取输入,并进行过滤。用于对来自非安全来源的变量进行验证,比如用户的输入。

    格式mixed filter_input(int $type , string $variable [, int $filter = FILTER_DEFAULT [,mixed $options]])

    返回值:如果成功,则返回被过滤的数据,如果失败,则返回false,如果variable参数未设置,则返回NULL。

    参数说明:

    参数描述
    type 必需。规定输入类型。INPUT_GET、INPUT_POST、INPUT_COOKIE、INPUT_ENV、INPUT_SERVER
    variable 必需。规定要过滤的变量。
    filter

    可选。规定要使用的过滤器的ID。默认是FILTER_DEFAULT。

    过滤器ID可以是ID名称(比如FILTER_VALIDATE_EMAIL),或ID号(比如 274)。

    PHP手册:http://www.php.net/manual/zh/filter.filters.php

    options 可选。规定包含标志/选项的数组。检查每个过滤器可能的标志和选项。

    示例:

    1 $_GET['search'] = 'foo'; // This has no effect on the filter_input
    2 
    3 $search_html = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_SPECIAL_CHARS);
    4 $search_url = filter_input(INPUT_GET, 'search', FILTER_SANITIZE_ENCODED);
    5 echo "You have searched for $search_html.
    ";
    6 echo "<a href='?search=$search_url'>Search again.</a>";
    7 
    8 //示例代码来自PHP手册中的评论
    9 //http://www.php.net/manual/zh/function.filter-input.php#99124

     

     

  • 相关阅读:
    linux之查找文件,目录命令
    linux文件操作常用命令
    linux打包解包压缩解压命令
    linux目录操作常用命令
    php读取文件行数方法
    前端入门之——javascript day8 DOM对象(DHTML)
    前端入门之——javascript day8
    前端入门之——css day5 作业。编写一个简单的网页
    前端入门之——css day4
    前端入门之——css day3
  • 原文地址:https://www.cnblogs.com/shockerli/p/php-filter_input.html
Copyright © 2011-2022 走看看