<?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.
- 浏览: 1825041 次
- 性别:
- 来自: 北京
文章分类
最新评论
-
wangyudong:
Wisdom RESTClient工具地址更新了哦https: ...
前端模拟POST发送数据-Chrome下的REST Client(接口测试利器) -
wangyudong:
很多REST Client是不支持自动化测试RESTful A ...
前端模拟POST发送数据-Chrome下的REST Client(接口测试利器) -
higkoo:
一个非常棒的系统,要是能支持LDAP等开源认证就完美了。
Cynthia 是个问题管理/BUG管理/任务管理/项目管理系统。 -
寻光之旅:
标签库使用有啥好处呢?
thinkphp 使用标签库的步骤 -
lgdjy123:
Android:TextView属性大全
filter扩展库简介
This extension filters data by either validating or sanitizing it. This is especially useful when the data source contains unknown (or foreign) data, like user supplied input. For example, this data may come from an HTML form.
There are two main types of filtering: validation and sanitization.
Validation
is used to validate or check if the data meets certain qualifications. For
example, passing in FILTER_VALIDATE_EMAIL
will
determine if the data is a valid email address, but will not change the data
itself.
Sanitization
will sanitize the data, so it may alter it by removing undesired characters. For
example, passing in FILTER_SANITIZE_EMAIL
will
remove characters that are inappropriate for an email address to contain. That
said, it does not validate the data.
Flags are optionally used with both
validation and sanitization to tweak behaviour according to need. For example,
passing in FILTER_FLAG_PATH_REQUIRED
while
filtering an URL will
require a path (like /foo in http://example.org/foo) to be
present.
这个扩展过滤器数据通过验证和消毒。这是特别有用,当数据源包含未知(或外国)数据,比如用户提供的输入。例如,这个数据可能来自一个HTML表单。 有两种主要类型的过滤:验证和数据消毒。 验证是用于验证或检查数据满足一定的条件。例如,通过在滤波器验证电子邮件将决定如果数据是一个有效的电子邮件地址,但不会改变数据本身。 将清洁卫生处理的数据,所以它可能改变它通过消除不受欢迎的人物。例如,通过在过滤净化电子邮件将删除字符不含有一个电子邮件地址。据说,它不验证数据。 旗帜是选择性地用于验证和数据消毒根据需要来调整行为。例如,passin
过滤的函数列表:
函数名 功能
filter_list 过滤器列表——返回一个列表的所有支持的过滤器
filter_id 过滤器ID -返回过滤器ID属于一个名叫过滤器
filter_has_var 过滤器已经var -检查如果变量指定类型的存在
filter_input 过滤输入——得到一个特定的外部变量的名称,并选择性地过滤它
filter_input_array 过滤输入数组——得到外部变量和选择性地过滤它们
filter_var 使用filter_var过滤变量指定的过滤器
filter_var_array 过滤器var数组——得到多个变量和选择性地过滤它们
函数示例:
filter_list
<?php print_r(filter_list()); ?> 以上例程的输出类似于: Array ( [0] => int [1] => boolean [2] => float [3] => validate_regexp [4] => validate_url [5] => validate_email [6] => validate_ip [7] => string [8] => stripped [9] => encoded [10] => special_chars [11] => unsafe_raw [12] => email [13] => url [14] => number_int [15] => number_float [16] => magic_quotes [17] => callback )
filter_list和filter_id结合
<?php echo "<pre>"; print_r(filter_list()); echo "</pre>"; foreach (filter_list() as $key => $value) { echo "<br>".$key."=".$value.'='.filter_id($value); } ?> 0=int=257 1=boolean=258 2=float=259 3=validate_regexp=272 4=validate_url=273 5=validate_email=274 6=validate_ip=275 7=string=513 8=stripped=513 9=encoded=514 10=special_chars=515 11=unsafe_raw=516 12=email=517 13=url=518 14=number_int=519 15=number_float=520 16=magic_quotes=521 17=callback=1024
filter_id
<?php $filters = filter_list(); foreach($filters as $filter_name) { echo $filter_name .": ".filter_id($filter_name) ."<br>"; } ?> Will result in: boolean: 258 float: 259 validate_regexp: 272 validate_url: 273 validate_email: 274 validate_ip: 275 string: 513 stripped: 513 encoded: 514 special_chars: 515 unsafe_raw: 516 email: 517 url: 518 number_int: 519 number_float: 520 magic_quotes: 521 callback: 1024
filter_has_var 函数效率:
To note: filter_has_var() is a bit faster than isset()
翻译:filter_has_var函数比isset()快一点
Please note that the function does not check the live
array, it actually checks the content received by php:
翻译:请注意,这个函数不检查包括数组,它只检查PHP接收到的内容
验证范例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 地址。
<?php if (!filter_input(INPUT_POST, 'email', FILTER_VALIDATE_EMAIL))
{ echo "E-Mail is not valid"; } else { echo "E-Mail is valid"; } ?> 输出类似: E-Mail is valid
过滤和验证字串的过滤类型详细请见PHP官方手册
写这篇内容的时候发现以前有朋友写过了,给个链接可以查看一下更多 http://blog.johnsonlu.org/phpfilter/
发表评论
-
Lumen写事件
2018-02-10 10:46 6581.在事件里面定义事件 <?php /** * ... -
PHP设计模式之状态机模式-实现业务流控制
2018-01-28 11:16 1663应用场景:在我们日常开发中经常会遇到各种状态的切换,例如电 ... -
装wampserver时显示计算机丢失MSVCR110.dll
2017-03-07 10:15 1073http://www.microsoft.com/zh-CN ... -
phpdocument的使用
2016-10-25 16:24 481官网: https://www.phpdoc.org/ ... -
微信调试方法
2016-05-24 09:25 7071. 在本地搭建类似LAMP的环境(或者WAMP)都行,目的 ... -
创建自己的composer包
2015-12-10 17:01 907创建一个composer/packagist包 在g ... -
微信nickname乱码及mysql编码格式设置(utf8mb4)
2015-12-08 12:24 1626将数据库的编码设置为utf8mb4_general_ci ... -
docker php 配置
2015-10-28 18:10 1032FROM php:5.6-fpm MAINTAINER S ... -
四种数据存储结构---顺序存储 链接存储 索引存储 散列存储
2015-08-15 21:54 1444存储结构分四类:顺序存储、链接存储、索引存储 和 散列存储。 ... -
分享PHP代码检查经验
2015-08-09 23:03 1512问: 团队十多人开发, 如何保证代码规范统一? http ... -
php过滤只匹配中英文字符串
2015-08-04 15:26 1812<?php $str = "php $ ... -
移除emoji内容
2015-08-04 14:52 1163public static function removeE ... -
php 敏感词过滤高级版
2015-08-04 10:20 1410前面介绍过一个过滤了 ... -
php过滤广告内容(兼职,QQ号,淘宝兼职,网址)
2015-08-04 10:17 1180如果你网站有评论那么你肯定会发现你网站经常会被一人注入广告了 ... -
写第一个PHP扩展, 实现计算数组的个数
2015-07-20 19:09 1261需求: 写第一个PHP扩展, 里面包含一个函数叫 maxw ... -
PHP常用设计模式单例, 工厂, 观察者, 责任链, 装饰, 策略,适配,桥接模式
2015-07-15 11:08 1534// 多态, 在JAVA中是这样用的, 其实在PHP当中可 ... -
php命令行界面
2015-07-16 17:03 886常用选项 php -v php -i ... -
使用phpdocument
2015-07-14 17:05 798pear install phpdoc phpDocumen ... -
将session存储到数据库中
2015-07-14 15:33 726CREATE TABLE sessions ( id ... -
正则表达式向前查找向后查找,环绕或零宽断言
2015-07-14 12:40 2253向前查找和向后查找 1. 向前查找:根据要匹配的字符序列后 ...
相关推荐
后来随着使用的逐渐深入,才知道在php中也可以使用内置的函数库过滤器filter来完成这些功能。 对于filter_var这个函数,如果验证通过则会返回验证对象,否则返回false。 语法 filter_var(variable, filter, options...
Arrays — Array 数组函数 Aspell — Aspell 函数(已废弃) BBCode — BBCode Functions BC math — BC math 高精度数学函数 bcompiler — BCompiler PHP 字节码编译器 Bzip2 — Bzip2 压缩函数 Calendar — ...
Array 数组函数 VI. Aspell 函数(已废弃) VII. BBCode Functions VIII. BC math 高精度数学函数 IX. BCompiler PHP 字节码编译器 X. Bzip2 压缩函数 XI. Calendar 日历函数 XII. CCVS API 函数(已废弃) XIII. ...
Arrays — Array 数组函数 Aspell — Aspell 函数(已废弃) BBCode — BBCode Functions BC math — BC math 高精度数学函数 bcompiler — BCompiler PHP 字节码编译器 Bzip2 — Bzip2 压缩函数 Calendar — ...
Array 数组函数 VI. Aspell 函数(已废弃) VII. BBCode Functions VIII. BC math 高精度数学函数 IX. BCompiler PHP 字节码编译器 X. Bzip2 压缩函数 XI. Calendar 日历函数 XII. CCVS API 函数(已废弃) ...
ThinkPHP借鉴了国外很多优秀的框架和模式,使用面向对象的开发结构和MVC模式,采用单一入口模式等,融合了Struts的 Action思想和JSP的TagLib(标签库)、RoR的ORM映射和ActiveRecord模式,封装了CURD和一些常用操作,...
ECMA 2009年12月推出了 JavaScript 5,除了增强基础函数库之外,还引入了严格运行时模式(Strict Runtime Modes)以避免代码中的常见错误。 严格模式 严格模式可用以下语句声明: "use strict;"该语句对以往的...
在以前,PHP需要验证数据,一般都是程序员自己通过正则表达式实现,而从PHP从5.2开始把原本的PCEL中的filter函数移到了内置库中,并做了不少强化,可以用这些函数实现对数据的过滤和验证。 数据来源及验证类型 PHP...
用法应用程序使用PrintSend()函数可以直接向打印机发送控制序列(换码序列),比如,设置打印文字的浓淡、打印页的四周空白等。不同类型的打印机使用不同的控制序列,你需要翻一翻你的打印机手册。由于PowerBuilder...
08 map函数filter函数 09 reduce函数 10 map reduce filter总结 11 内置函数part1 第17章 01 课前吹牛 02 zip方法 03 max和min高级使用 04 其他内置函数 05 文件操作的其他模式 第18章 01 上节课复习 02 文件...
10、静态变量和实例变量的区别? 15 11、面向对象的基本特征是什么? 15 12、作用域public,private,protected,以及不写时的区别? 16 13、Overload和Override的区别。 16 14、构造器可不可以被重载或重写? 16 15、...
4.5.6 库文件和搜索目录 4.6 Makefile伪目标 4.7 强制目标(没有命令或依赖的规则) 4.8 空目标文件 4.9 Makefile的特殊目标 4.10 多目标 4.11 多规则目标 4.12 静态模式 4.12.1 静态模式规则的语法 4.12.2...
4.5.6 库文件和搜索目录 4.6 Makefile伪目标 4.7 强制目标(没有命令或依赖的规则) 4.8 空目标文件 4.9 Makefile的特殊目标 4.10 多目标 4.11 多规则目标 4.12 静态模式 4.12.1 静态模式规则的语法 4.12.2 静态模式...
4.5.6 库文件和搜索目录 4.6 Makefile伪目标 4.7 强制目标(没有命令或依赖的规则) 4.8 空目标文件 4.9 Makefile的特殊目标 4.10 多目标 4.11 多规则目标 4.12 静态模式 4.12.1 静态模式规则的语法 4.12.2...
4.5.6 库文件和搜索目录 4.6 Makefile伪目标 4.7 强制目标(没有命令或依赖的规则) 4.8 空目标文件 4.9 Makefile的特殊目标 4.10 多目标 4.11 多规则目标 4.12 静态模式 4.12.1 静态模式规则的语法 4.12.2...
虽然在VBScript中我们可以直接访问JScript数组相关的 方法和属性,但没有直接访问JScript数组的单个元素的方法。也就是说,我们可以在VBScript 脚本中 读取JScript数组的长度属性,如下所示: <br> x = ...
2.8 强制类型转换和类型约定 46 2.9 字符串资源 47 2.10 测试条件 47 2.10.1 if语句 47 2.10.2 case语句 48 2.11 循环 49 2.11.1 for循环 49 2.11.2 while循环 49 2.11.3 repeat...until 50 2.11.4 Break()过程 50 ...
KalmanFilter类还包含预测步骤以及卡尔曼滤波器更新步骤(激光雷达)和扩展卡尔曼滤波器更新步骤(雷达)的功能。 tools.cpp是相对简单的。 它实现了两个函数来计算均方根误差和雅可比矩阵。 在当地建立环境 该项目...
定义数组和对象 1.9节.在ActionScript中设置变量的作用域 1.10节.在ActionScript中创建组件 1.11节.使用事件冒泡机制 1.12节.使用代码隐藏模式分离MXML和ActionScript 1.13节.组件属性绑定 1.14节.使用自定义事件...
跟我一起写 Makefile 作者:陈皓 整理:祝冬华 ...第十二部分使用make更新函数库文件 (75) 一、函数库文件的成员 (75) 二、函数库成员的隐含规则 (75) 三、函数库文件的后缀规则 (76) 第十三部分后序 (77)