search
Homephp教程php手册php filter_input函数

php filter_input函数

Jun 13, 2016 am 11:18 AM
filterinputphpbuilt-infunctionvariableexistTutorialmoduleofverify

 

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


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

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

Dreamweaver Mac version

Dreamweaver Mac version

Visual web development tools

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.