Home  >  Article  >  Backend Development  >  php filter_input函数_PHP教程

php filter_input函数_PHP教程

WBOY
WBOYOriginal
2016-07-20 11:01:361160browse

 

在 php教程5.2 中,内置了filter 模块,用于变量的验证和过滤。

过滤变量等操作可以参看我原先提及的,这里我们看下如何直接过滤用户输入的内容。

fliter 模块对应的 filter_input 函数使用起来非常的简单,例如我们过滤用户输入名为 sample 的 get 参数为整型,那么可以这样写

filter_input(input_get, "sample", filter_sanitize_number_int);
filter_input 的参数分别是用户输入类型、对应的输入名称、以及过滤(验证)常量。目前 filter_input 支持下面几种用户输入

input_get // 对应 $_get
input_post // 对应 $_post
input_cookie // 对应 $_cookie
input_server // 对应 $_server
input_env // 对应 $_env
配合内置提供的各种验证标记符,就可以解决类似的用户输入过滤等“体力活”。

最后,还是需要再提下 filter 的个不大不小的陷阱

filter_var('abc', filter_validate_boolean); // bool(false)
filter_var('0',   filter_validate_boolean); // bool(false)


 php arch 上重新提及 fliter 模块,的确这个模块能节省我们不少的时间,这里再次整理下。

$_get 和 $_post 等用户提供的数据如果使用不当,如验证、过滤不全面,就很容易造成安全问题。通常情况下,我们会编写“一坨”正则来验证数据格式是否合法。


www.bkjia.comtruehttp://www.bkjia.com/PHPjc/445413.htmlTechArticle在 php教程5.2 中,内置了filter 模块,用于变量的验证和过滤。 过滤变量等操作可以参看我原先提及的,这里我们看下如何直接过滤用户输入...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn