php过滤器

几乎所有 web 应用程序都依赖外部的输入。这些数据通常来自用户或其他应用程序(比如 web 服务)。通过使用过滤器,为了能够确保应有程序获得正确的输入类型,所以应该始终对外部数据进行过滤!

php中的过滤输入函数就是Filter,主要有:

  • filter_var() – 通过一个指定的过滤器来过滤单一的变量
  • filter_var_array() – 通过相同的或不同的过滤器来过滤多个变量
  • filter_input – 获取一个输入变量,并对它进行过滤
  • filter_input_array – 获取多个输入变量,并通过相同的或不同的过滤器对它们进行过滤

另外经常用到的Filter函数及filter过滤参数有:

过滤扩展功能有两种过滤器:Sanitizing 和 Logical。

Sanitizing 过滤器只是允许或禁止字符串中的字符并将清理后的字符串作为结果返回。无论您将哪种数据格式传入这些函数,它们将始终返回字符串。对于特定类型的使用,这是至关重要的,因为您可以阻止用户发送不适当的输入并导致异常结果。例如,用户可以发现文本块的输入被返回到以下页面上并且要利用那些返回信息。如果清理输入,则将删除输入的所有危险部分。

Logical 过滤器将对变量执行测试并根据测试提供 true 或 false 结果。然后您可以使用结果来决定如何处理数据或获得用户的地址。这种过滤器的简单示例是验证年龄。逻辑测试还可以针对类似 Perl 的正则表达式进行测试。
<?php

echo “You are ” . filter_var($_GET[‘1′], FILTER_SANITIZE_STRING) . “.<br>\n”;
echo “Your favorite color is ” . filter_var($_GET[‘2′], FILTER_SANITIZE_STRING) .
“.<br>\n”;
echo “The airspeed of an unladen swallow is ” . filter_var($_GET[‘3′], FILTER_SANITIZE_STRING)
. “.<br>\n”;
?>
用 filter_var() 函数来清理输入并使其有效并且安全。在这种情况下,使用选项 FILTER_SANITIZE_STRING,该选项将获取输入、删除所有 HTML 标记并选择性地编码或删除特定字符。

由于它将除去 HTML 标记,因此尝试运行 JavaScript 将失败,并且从脚本中获得更适当的结果。

再补充些

PHP: indicates the earliest version of PHP that supports the function.

Function Description PHP
filter_has_var() Checks if a variable of a specified input type exist 5
filter_id() Returns the ID number of a specified filter 5
filter_input() Get input from outside the script and filter it 5
filter_input_array() Get multiple inputs from outside the script and filters them 5
filter_list() Returns an array of all supported filters 5
filter_var_array() Get multiple variables and filter them 5
filter_var() Get a variable and filter it 5

PHP Filters

ID Name Description
FILTER_CALLBACK Call a user-defined function to filter data
FILTER_SANITIZE_STRING Strip tags, optionally strip or encode special characters
FILTER_SANITIZE_STRIPPED Alias of “string” filter
FILTER_SANITIZE_ENCODED URL-encode string, optionally strip or encode special characters
FILTER_SANITIZE_SPECIAL_CHARS HTML-escape ‘”<>& and characters with ASCII value less than 32
FILTER_SANITIZE_EMAIL Remove all characters, except letters, digits and !#$%&’*+-/=?^_`{|}~@.[]
FILTER_SANITIZE_URL Remove all characters, except letters, digits and $-_.+!*'(),{}|\\^~[]`<>#%”;/?:@&=
FILTER_SANITIZE_NUMBER_INT Remove all characters, except digits and +-
FILTER_SANITIZE_NUMBER_FLOAT Remove all characters, except digits, +- and optionally .,eE
FILTER_SANITIZE_MAGIC_QUOTES Apply addslashes()
FILTER_UNSAFE_RAW Do nothing, optionally strip or encode special characters
FILTER_VALIDATE_INT Validate value as integer, optionally from the specified range
FILTER_VALIDATE_BOOLEAN Return TRUE for “1”, “true”, “on” and “yes”, FALSE for “0”, “false”, “off”, “no”, and “”, NULL otherwise
FILTER_VALIDATE_FLOAT Validate value as float
FILTER_VALIDATE_REGEXP Validate value against regexp, a Perl-compatible regular expression
FILTER_VALIDATE_URL Validate value as URL, optionally with required components
FILTER_VALIDATE_EMAIL Validate value as e-mail
FILTER_VALIDATE_IP Validate value as IP address, optionally only IPv4 or IPv6 or not from private or reserved ranges

需要注意的是:如果成功的话将返回转换过之后的格式,如果失败(格式不符,值超出范围等等。)将返回FALSE,如果变量没有设置将返回NULL。使用FILTER_NULL_ON_FAILURE标记后将会在失败的时候返回NULL,变量没有设置的时候返回FALSE。

Leave a Reply

 

 

 

You can use these HTML tags

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Protected by WP Anti Spam