php 5.2 filter

在PHP 5.2最新版本中,在对输入检查的函数方面,多了新的功能,默认是开启的,减少了很多写代码的工作量
,在IBM DW的 http://www.ibm.com/developerworks/cn/opensource/os-php-v522/index.html 上有很好的介绍,下面笔记并补充之


过滤扩展功能有两种过滤器: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.

FunctionDescriptionPHP
filter_has_var()Checks if a variable of a specified input type exist5
filter_id()Returns the ID number of a specified filter5
filter_input()Get input from outside the script and filter it5
filter_input_array()Get multiple inputs from outside the script and filters them5
filter_list()Returns an array of all supported filters5
filter_var_array()Get multiple variables and filter them5
filter_var()Get a variable and filter it5


PHP Filters

ID NameDescription
FILTER_CALLBACKCall a user-defined function to filter data
FILTER_SANITIZE_STRINGStrip tags, optionally strip or encode special characters
FILTER_SANITIZE_STRIPPEDAlias of "string" filter
FILTER_SANITIZE_ENCODEDURL-encode string, optionally strip or encode special characters
FILTER_SANITIZE_SPECIAL_CHARSHTML-escape '"<>& and characters with ASCII value less than 32
FILTER_SANITIZE_EMAILRemove all characters, except letters, digits and !#$%&'*+-/=?^_`{|}~@.[]
FILTER_SANITIZE_URLRemove all characters, except letters, digits and $-_.+!*'(),{}|//^~[]`<>#%";/?:@&=
FILTER_SANITIZE_NUMBER_INTRemove all characters, except digits and +-
FILTER_SANITIZE_NUMBER_FLOATRemove all characters, except digits, +- and optionally .,eE
FILTER_SANITIZE_MAGIC_QUOTESApply addslashes()
FILTER_UNSAFE_RAWDo nothing, optionally strip or encode special characters
FILTER_VALIDATE_INTValidate value as integer, optionally from the specified range
FILTER_VALIDATE_BOOLEANReturn TRUE for "1", "true", "on" and "yes", FALSE for "0", "false", "off", "no", and "", NULL otherwise
FILTER_VALIDATE_FLOATValidate value as float
FILTER_VALIDATE_REGEXPValidate value against regexp, a Perl-compatible regular expression
FILTER_VALIDATE_URLValidate value as URL, optionally with required components
FILTER_VALIDATE_EMAILValidate value as e-mail
FILTER_VALIDATE_IPValidate value as IP address, optionally only IPv4 or IPv6 or not from private or reserved ranges


检测判断的例子

 
1<html>  
2<head></head>  
3<body  
4    <form action="example04.php" method="post" >  
5    Enter your age: <input name="age" size="2">  
6    <input type="submit" name="submit" value="Go">  
7    </form>  
8</body>  
9</html> 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. <html>  
  2. <head></head>  
  3. <body  
  4. <form action="example04.php" method="post" >  
  5. Enter your age: <input name="age" size="2">  
  6. <input type="submit" name="submit" value="Go">  
  7. </form>  
  8. </body>  
  9. </html>  


处理脚本:

 
1<?php  
2if (!filter_has_var(INPUT_POST, 'submit')) {  
3    echo "form";  
4    // include the form.  
5}  
6 
7$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT);  
8if (is_null($age)) {  
9    echo "The 'age' field is required.<br />";  
10elseif ($age === FALSE) {  
11    echo "Please enter a valid age.<br />";  
12else {  
13    echo "Welcome.<br/>";  
14}  
15?> 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. <?php  
  2. if (!filter_has_var(INPUT_POST, 'submit')) {  
  3. echo "form";  
  4. // include the form.  
  5. }  
  6. $age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT);  
  7. if (is_null($age)) {  
  8. echo "The 'age' field is required.<br />";  
  9. elseif ($age === FALSE) {  
  10. echo "Please enter a valid age.<br />";  
  11. else {  
  12. echo "Welcome.<br/>";  
  13. }  
  14. ?>  


filter_has_var检测给定的变量是否存在。它不会做任何处理只会告诉程序变量是否已经设置。相当于isset($_POST[‘submit’])。filter_input会取得一个变量返回处理好的数据。在上面的例子中会返回一个整数。

如果是要返回一个处于一定范围之内的值,假设是7-77岁之间的人。可以指定一个最大值和一个最小值。

 
1<?php  
2if (!filter_has_var(INPUT_POST, 'submit')) {  
3    echo "form";  
4    // include the form.  
5}  
6$options = array('options'=> array('min_range'=>7, 'min_range'=>77));  
7$age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT, $options);  
8 
9if (is_null($age)) {  
10    echo "The 'age' field is required.<br />";  
11elseif ($age === FALSE) {  
12    echo "You must be enter a valid age and be between 7 and 77 years old.<br />";  
13else {  
14    echo "Welcome.<br/>";  
15}  
16?> 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. <?php  
  2. if (!filter_has_var(INPUT_POST, 'submit')) {  
  3. echo "form";  
  4. // include the form.  
  5. }  
  6. $options = array('options'=> array('min_range'=>7, 'min_range'=>77));  
  7. $age = filter_input(INPUT_POST, 'age', FILTER_VALIDATE_INT, $options);  
  8. if (is_null($age)) {  
  9. echo "The 'age' field is required.<br />";  
  10. elseif ($age === FALSE) {  
  11. echo "You must be enter a valid age and be between 7 and 77 years old.<br />";  
  12. else {  
  13. echo "Welcome.<br/>";  
  14. }  
  15. ?>  


如果是要判断一个有效的邮件地址的话,可以这样:

 
1$email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL); 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. $email = filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL);  


如果邮件地址不正确或者是空的话$email的值将为FALSE。

过滤判断的例子

 
1<html>  
2<head></head>  
3<body>  
4    <form action="example05.php" method="post" >  
5    Enter your name: <input name="name" size="50">  
6    <input type="submit" name="submit" value="Go">  
7    </form>  
8</body>  
9</html> 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. <html>  
  2. <head></head>  
  3. <body>  
  4. <form action="example05.php" method="post" >  
  5. Enter your name: <input name="name" size="50">  
  6. <input type="submit" name="submit" value="Go">  
  7. </form>  
  8. </body>  
  9. </html>  


下面的filter_input函数将自动过滤并返回适当的值:

 
1<?php  
2if (!filter_has_var(INPUT_POST, 'submit')) {  
3    echo "form";  
4    // include the form.  
5}  
6$name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);  
7if (is_null($name)) {  
8    echo "The 'name' field is required.<br />";  
9else {  
10    echo "Hello $name.<br/>";  
11}  
12?> 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. <?php  
  2. if (!filter_has_var(INPUT_POST, 'submit')) {  
  3. echo "form";  
  4. // include the form.  
  5. }  
  6. $name = filter_input(INPUT_POST, 'name', FILTER_SANITIZE_SPECIAL_CHARS);  
  7. if (is_null($name)) {  
  8. echo "The 'name' field is required.<br />";  
  9. else {  
  10. echo "Hello $name.<br/>";  
  11. }  
  12. ?>  


如果接收到的name值是:
Johnny Wei&szlig;müller <b>Jr</b>

FILTER_SANITIZE_SPECIAL_CHARS 将会返回:
Hello Johnny Wei&#223;müller <b>Jr</b>.

一个更好的过滤写法:

 
1$name = filter_input(INPUT_POST,   
2                     'name',  
3                     FILTER_SANITIZE_STRING,   
4                     FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW); 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. $name = filter_input(INPUT_POST,  
  2. 'name',  
  3. FILTER_SANITIZE_STRING,  
  4. FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW);  


输出:
Hello Johnny Weißmüller Jr.

这里的函数还有很多选项,如果你想了解更多的细节可以查看Filter的文档。

如何一次处理所有输入?

 
1<html>  
2<head></head>  
3<body  
4    <form action="example07.php" method="post" >  
5    Name: <input name="name" size="50"><br />  
6    Email: <input name="email" size="50"><br />  
7    Homepage: <input name="homepage" size="50"><br />  
8    Age: <input name="age" size="4"><br />  
9    Income: <input name="income" size="50"><br />  
10    Your two favourites languages:  
11    <select name="favourites[]" size="6" multiple>  
12        <option value="haskell">haskell</option>  
13        <option value="r">R</option>  
14        <option value="lua">Lua</option>  
15        <option value="pike">Pike</option>  
16        <option value="rebol">Rebol</option>  
17        <option value="php">PHP</option>  
18    </select><br />  
19    <input type="submit" name="submit" value="Go">  
20    </form>  
21</body>  
22</html> 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. <html>  
  2. <head></head>  
  3. <body  
  4. <form action="example07.php" method="post" >  
  5. Name: <input name="name" size="50"><br />  
  6. Email: <input name="email" size="50"><br />  
  7. Homepage: <input name="homepage" size="50"><br />  
  8. Age: <input name="age" size="4"><br />  
  9. Income: <input name="income" size="50"><br />  
  10. Your two favourites languages:  
  11. <select name="favourites[]" size="6" multiple>  
  12. <option value="haskell">haskell</option>  
  13. <option value="r">R</option>  
  14. <option value="lua">Lua</option>  
  15. <option value="pike">Pike</option>  
  16. <option value="rebol">Rebol</option>  
  17. <option value="php">PHP</option>  
  18. </select><br />  
  19. <input type="submit" name="submit" value="Go">  
  20. </form>  
  21. </body>  
  22. </html>  


处理程序:

 
1<?php  
2if (!filter_has_var(INPUT_POST, 'submit')) {  
3    echo "form";  
4    // include the form.  
5}  
6 
7$defs = array(  
8    'name'       => array('filter'=>FILTER_SANITIZE_STRING,  
9                    'flags' => FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW),  
10    'email'      => FILTER_VALIDATE_EMAIL,  
11    'homepage'   => FILTER_VALIDATE_URL,  
12    'age'        => array(  'filter' => FILTER_VALIDATE_INT,  
13                            'options'=> array('min_range'=>7, 'min_range'=>77)),  
14    'income'     => FILTER_VALIDATE_FLOAT,  
15    'favourites' => array(  
16                        'filter' => FILTER_SANITIZE_STRING,  
17                        'flags'  => FILTER_REQUIRE_ARRAY  
18                    ),  
19          );  
20 
21$input = filter_input_array(INPUT_POST, $defs);  
22 
23if ($input['age'] === FALSE) {  
24    exit("You must be between 7 and 77 years old.");  
25}  
26 
27if (is_null($input['favourites'])) {  
28    exit("You have to choose two or more languages.");  
29}  
30 
31if (!in_array('PHP'$inputs['favourites'])) {  
32    exit("You don't like PHP!");  
33}  
34 
35/*Other checks for required values */ 
36?> 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. <?php  
  2. if (!filter_has_var(INPUT_POST, 'submit')) {  
  3. echo "form";  
  4. // include the form.  
  5. }  
  6. $defs = array(  
  7. 'name'       => array('filter'=>FILTER_SANITIZE_STRING,  
  8. 'flags' => FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW),  
  9. 'email'      => FILTER_VALIDATE_EMAIL,  
  10. 'homepage'   => FILTER_VALIDATE_URL,  
  11. 'age'        => array(  'filter' => FILTER_VALIDATE_INT,  
  12. 'options'=> array('min_range'=>7, 'min_range'=>77)),  
  13. 'income'     => FILTER_VALIDATE_FLOAT,  
  14. 'favourites' => array(  
  15. 'filter' => FILTER_SANITIZE_STRING,  
  16. 'flags'  => FILTER_REQUIRE_ARRAY  
  17. ),  
  18. );  
  19. $input = filter_input_array(INPUT_POST, $defs);  
  20. if ($input['age'] === FALSE) {  
  21. exit("You must be between 7 and 77 years old.");  
  22. }  
  23. if (is_null($input['favourites'])) {  
  24. exit("You have to choose two or more languages.");  
  25. }  
  26. if (!in_array('PHP'$inputs['favourites'])) {  
  27. exit("You don't like PHP!");  
  28. }  
  29. /*Other checks for required values */  
  30. ?>  


正如上面例子中的,通过一个函数就能处理所有的变量。唯一不同的就是事先定义一个对应的数组。需要注意的是数组中选项的正确性。

这样的做法不但增加了程序的易读性,并且如果要添加移除或者修改处理规则也会非常方便。

更复杂的处理

在下面的处理“favourites”变量时用到了用户自定义函数。"options"指定一个用户自定义函数通过定义callback来实现,语法和PHP的call_user_func一样。

 
1<?php  
2class language {  
3    function __construct($name) {  
4        $this->name = $name;  
5    }  
6}  
7 
8function check_languages($var) {  
9    static $called = 0;  
10    $called++;  
11    echo "called: $called: $var<br />";  
12    $var = filter_var($var, FILTER_SANITIZE_STRIPPED);  
13    $l = new language($var);  
14    return $l;  
15}  
16 
17if (!filter_has_var(INPUT_POST, 'submit')) {  
18    echo "form";  
19    // include the form.  
20}  
21 
22$defs = array(  
23    'name'       => array('filter'=>FILTER_SANITIZE_STRING,  
24                    'flags' => FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW),  
25    'email'      => FILTER_VALIDATE_EMAIL,  
26    'homepage'   => FILTER_VALIDATE_URL,  
27    'age'        => FILTER_VALIDATE_INT,  
28    'income'     => FILTER_VALIDATE_FLOAT,  
29    'favourites' => array(  
30                            'filter' => FILTER_CALLBACK,  
31                            'options'  => 'check_languages' 
32                    ),  
33          );  
34 
35$input = filter_input_array(INPUT_POST, $defs);  
36 
37if ($input['age'] === FALSE) {  
38    exit("You must be between 7 and 77 years old.");  
39}  
40 
41if (is_null($input['favourites'])) {  
42    exit("You have to choose two or more languages.");  
43}  
44 
45echo "Your favourite languages:<br /><ul>";  
46foreach ($input['favourites'as $lecho '<li>' . $l->name . "</li>";  
47echo '</ul>';  
48?> 
  view plain | print | copy to clipboard | ?
[php]  view plain copy
  1. <?php  
  2.         class language {  
  3.         function __construct($name) {  
  4.         $this->name = $name;  
  5.         }  
  6.         }  
  7.         function check_languages($var) {  
  8.         static $called = 0;  
  9.         $called++;  
  10.         echo "called: $called: $var<br />";  
  11.         $var = filter_var($var, FILTER_SANITIZE_STRIPPED);  
  12.         $l = new language($var);  
  13.         return $l;  
  14.         }  
  15.         if (!filter_has_var(INPUT_POST, 'submit')) {  
  16.         echo "form";  
  17.         // include the form.  
  18.         }  
  19.         $defs = array(  
  20.         'name'       => array('filter'=>FILTER_SANITIZE_STRING,  
  21.         'flags' => FILTER_FLAG_ENCODE_HIGH|FILTER_FLAG_ENCODE_LOW),  
  22.         'email'      => FILTER_VALIDATE_EMAIL,  
  23.         'homepage'   => FILTER_VALIDATE_URL,  
  24.         'age'        => FILTER_VALIDATE_INT,  
  25.         'income'     => FILTER_VALIDATE_FLOAT,  
  26.         'favourites' => array(  
  27.         'filter' => FILTER_CALLBACK,  
  28.         'options'  => 'check_languages'  
  29.         ),  
  30.         );  
  31.         $input = filter_input_array(INPUT_POST, $defs);  
  32.         if ($input['age'] === FALSE) {  
  33.         exit("You must be between 7 and 77 years old.");  
  34.         }  
  35.         if (is_null($input['favourites'])) {  
  36.         exit("You have to choose two or more languages.");  
  37.         }  
  38.         echo "Your favourite languages:<br /><ul>";  
  39.         foreach ($input['favourites'as $lecho '<li>' . $l->name . "</li>";  
  40.         echo '</ul>';  
  41.         ?>  


如果参数是标准参数,函数将只会别调用一次,如果变量是一个数组,将会自动被数组的每个成员调用一次。

当使用callback的时候Filter不会转换或者验证输入数据。但是上面例子中的filter_var可以在callback函数或者方法中使用。
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值