<?php $_GET['test'] = 1; echo filter_has_var(INPUT_GET, 'test') ? 'Yes' : 'No'; ?> would say "No", unless the parameter was actually in the querystring. Also, if the input var is empty, it will say Yes.
?
?
?
?
验证范例1(验证邮箱):
?
<?php $email_a = 'joe@example.com'; $email_b = 'bogus'; if (filter_var($email_a, FILTER_VALIDATE_EMAIL)) { echo "This (email_a) email address is considered valid."; } if (filter_var($email_b, FILTER_VALIDATE_EMAIL)) { echo "This (email_b) email address is considered valid."; } ?>
?以上程序输出:
?
This (email_a) email address is considered valid.
?
验证范例2(验证IP)
?
<?php $ip_a = '127.0.0.1'; $ip_b = '42.42'; if (filter_var($ip_a, FILTER_VALIDATE_IP)) { echo "This (ip_a) IP address is considered valid."; } if (filter_var($ip_b, FILTER_VALIDATE_IP)) { echo "This (ip_b) IP address is considered valid."; } ?>
?以上程序输出:
?
This (ip_a) IP address is considered valid.
?
验证范例3(通过选项来过滤变量):
?
<?php $int_a = '1'; $int_b = '-1'; $int_c = '4'; $options = array( 'options' => array( 'min_range' => 0, 'max_range' => 3, ) ); if (filter_var($int_a, FILTER_VALIDATE_INT, $options) !== FALSE) { echo "This (int_a) integer is considered valid (between 0 and 3).\n"; } if (filter_var($int_b, FILTER_VALIDATE_INT, $options) !== FALSE) { echo "This (int_b) integer is considered valid (between 0 and 3).\n"; } if (filter_var($int_c, FILTER_VALIDATE_INT, $options) !== FALSE) { echo "This (int_c) integer is considered valid (between 0 and 3).\n"; } $options['options']['default'] = 1; if (($int_c = filter_var($int_c, FILTER_VALIDATE_INT, $options)) !== FALSE) { echo "This (int_c) integer is considered valid (between 0 and 3) and is $int_c."; } ?>
?以上程序会输出:
?
This (int_a) integer is considered valid (between 0 and 3). This (int_c) integer is considered valid (between 0 and 3) and is 1.
?
消失有害字符并且验证示例1:
?
<?php $a = 'joe@example.org'; $b = 'bogus - at - example dot org'; $c = '(bogus@example.org)'; $sanitized_a = filter_var($a, FILTER_SANITIZE_EMAIL); if (filter_var($sanitized_a, FILTER_VALIDATE_EMAIL)) { echo "This (a) sanitized email address is considered valid.\n"; } $sanitized_b = filter_var($b, FILTER_SANITIZE_EMAIL); if (filter_var($sanitized_b, FILTER_VALIDATE_EMAIL)) { echo "This sanitized email address is considered valid."; } else { echo "This (b) sanitized email address is considered invalid.\n"; } $sanitized_c = filter_var($c, FILTER_SANITIZE_EMAIL); if (filter_var($sanitized_c, FILTER_VALIDATE_EMAIL)) { echo "This (c) sanitized email address is considered valid.\n"; echo "Before: $c\n"; echo "After: $sanitized_c\n"; } ?>
?以上程序会输出:
?
This (a) sanitized email address is considered valid. This (b) sanitized email address is considered invalid. This (c) sanitized email address is considered valid. Before: (bogus@example.org) After: bogus@example.org
?
下面介绍一下filter_input,摘自百度百科:
?
定义和用法
filter_input() 函数从脚本外部获取输入,并进行过滤。
本函数用于对来自非安全来源的变量进行验证,比如用户的输入。
本函数可从各种来源获取输入:
INPUT_GET
INPUT_POST
INPUT_COOKIE
INPUT_ENV
INPUT_SERVER
INPUT_SESSION (Not yet implemented)
INPUT_REQUEST (Not yet implemented)
如果成功,则返回被过滤的数据,如果失败,则返回 false,如果?variable?参数未设置,则返回 NULL。
语法
filter_input(input_type, variable, filter, options)
input_type | 必需。规定输入类型。参见上面的列表中可能的类型。 |
variable | 规定要过滤的变量。 |
filter | 可选。规定要使用的过滤器的 ID。默认是 FILTER_SANITIZE_STRING。? 请参见完整的 PHP Filter 函数参考手册,获得可能的过滤器。? 过滤器 ID 可以是 ID 名称 (比如 FILTER_VALIDATE_EMAIL),或 ID 号(比如 274)。 |
options | 规定包含标志/选项的数组。检查每个过滤器可能的标志和选项。 |
例子
在本例中,我们使用 filter_input() 函数来过滤一个 POST 变量。所接受的 POST 变量是合法的 e-mail 地址。
?
{ echo "E-Mail is not valid"; } else { echo "E-Mail is valid"; } ?> 输出类似: E-Mail is valid?
过滤和验证字串的过滤类型详细请见PHP官方手册
写这篇内容的时候发现以前有朋友写过了,给个链接可以查看一下更多?http://blog.johnsonlu.org/phpfilter/