


Take you through PHP filters in three minutes (detailed examples)
In the previous article, I brought you "PHP Form Learning: The Use and Differences of $_GET and $_POST Variables", which detailed the differences between $_GET variables in PHP and Knowledge about the $_POST variable. In this article, we will take a look at the knowledge about filters in PHP. I hope it can help you!
In the previous article we learned about $_GET
variables and $_POST
variables, which were mentioned Regarding security issues, the PHP filters discussed in this article are used to verify and filter data from non-secure sources, such as user input. Next, let’s take a look at the relevant knowledge of filters in PHP. Let’s take a look.
PHP filter
What is a filter, you can first simply understand the filter as Filter out unsafe data. So why do we use Weiwei? In our daily development, almost all web applications rely on external input. These data usually come from other applications like web services or from users. Through the use of filters we can ensure that the application gets the correct input type.
We should filter external data like input data from forms, cookies, server variables, database query results, etc. It is important to filter input, so we need to use filters .
PHP filters are used to validate and filter data from non-secure sources. They are an important part of any web application when testing, validating and filtering user input or custom data. It is designed to It makes data processing easier and faster.
Functions and filters
When we need to filter variables, we can use many filter functions: filter_var( )
Filter a single variable through a specified filter; filter_var_array()
Filter multiple variables through the same or different filters; filter_input
Get an input variable , and filter it;filter_input_array
Get multiple input variables and filter them through the same or different filters.
Next, let’s take an example to verify an integer through the filter_var()
function. The example is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $int = 123; if(!filter_var($int, FILTER_VALIDATE_INT)) { echo("不是一个合法的整数"); } else { echo("是个合法的整数"); } ?>
Output result:
The above example verifies an integer through the filter_var() filter function. Next, let’s take a look at the two commonly used filters.
<strong>Validating</strong>
Filter: Used to validate user input, with strict format rules (such as URL or E -Mail validation), returning the expected type if successful, or FALSE if failed.<strong>Sanitizing</strong>
Filter: used to allow or prohibit specified characters in the string, no data format rules , always returns a string.
Options and flags
Options and flags are used to add additional filtering to the specified filter options. Different filters have different options and flags.
Next let's look at an example using filter_var()
and "min_range"
and "max_range"
options to verify an integer, The example is as follows:
<?php header("Content-type:text/html;charset=utf-8"); $var=300; $int_options = array( "options"=>array ( "min_range"=>0, //最小值 "max_range"=>256 //最大值 ) ); if(!filter_var($var, FILTER_VALIDATE_INT, $int_options)) { echo("不是一个合法的整数"); } else { echo("是个合法的整数"); } ?>
Output result:
In the above example, it is important to note that: just like the above code, the options must into a related array called "options". If using flags, they don't need to be in an array. Since the integer is "300", it is not within the specified range, so the output is as above.
Validating input
Next let’s try to validate the input from the form. The first thing we need to do is confirm that the input data we are looking for exists. Then we use the filter_input()
function to filter the input data.
Next, let’s take an example to see how the input variable "email" is passed to the PHP page using GET. The example is as follows:
<?php header("Content-type:text/html;charset=utf-8"); if(!filter_has_var(INPUT_GET, "email")) { echo("没有 email 参数"); } else { if (!filter_input(INPUT_GET, "email", FILTER_VALIDATE_EMAIL)) { echo "不是一个合法的 E-Mail"; } else { echo "是一个合法的 E-Mail"; } } ?>
Output result:
What we need to pay attention to is: the above example has an input variable (email) transmitted through the "GET" method. Check whether there is an "email" input variable of the "GET" type. If there is an input variable , to check whether it is a valid e-mail address.
净化输入
让我们试着清理一下从表单传来的 URL。首先,我们要确认是否存在我们正在查找的输入数据。然后,我们用 filter_input()
函数来净化输入数据。
下面我们通过示例来看一下输入变量 "url" 被传到 PHP 页面,示例如下:
<?php header("Content-type:text/html;charset=utf-8"); if(!filter_has_var(INPUT_GET, "url")) { echo("没有 url 参数"); } else { $url = filter_input(INPUT_GET, "url", FILTER_SANITIZE_URL); echo $url; } ?>
输出结果:
其中我们需要注意的是:
FILTER_SANITIZE_URL 过滤器删除字符串中所有非法的 URL 字符。上面的实例有一个通过 "GET" 方法传送的输入变量 (url):检测是否存在 "GET" 类型的 "url" 输入变量,如果存在此输入变量,对其进行净化(删除非法字符),并将其存储在 $url 变量中。
大家如果感兴趣的话,可以点击《PHP视频教程》进行更多关于PHP知识的学习。
The above is the detailed content of Take you through PHP filters in three minutes (detailed examples). For more information, please follow other related articles on the PHP Chinese website!

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

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

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

If you are an experienced PHP developer, you might have the feeling that you’ve been there and done that already.You have developed a significant number of applications, debugged millions of lines of code, and tweaked a bunch of scripts to achieve op

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

JWT is an open standard based on JSON, used to securely transmit information between parties, mainly for identity authentication and information exchange. 1. JWT consists of three parts: Header, Payload and Signature. 2. The working principle of JWT includes three steps: generating JWT, verifying JWT and parsing Payload. 3. When using JWT for authentication in PHP, JWT can be generated and verified, and user role and permission information can be included in advanced usage. 4. Common errors include signature verification failure, token expiration, and payload oversized. Debugging skills include using debugging tools and logging. 5. Performance optimization and best practices include using appropriate signature algorithms, setting validity periods reasonably,

A string is a sequence of characters, including letters, numbers, and symbols. This tutorial will learn how to calculate the number of vowels in a given string in PHP using different methods. The vowels in English are a, e, i, o, u, and they can be uppercase or lowercase. What is a vowel? Vowels are alphabetic characters that represent a specific pronunciation. There are five vowels in English, including uppercase and lowercase: a, e, i, o, u Example 1 Input: String = "Tutorialspoint" Output: 6 explain The vowels in the string "Tutorialspoint" are u, o, i, a, o, i. There are 6 yuan in total

This tutorial demonstrates how to efficiently process XML documents using PHP. XML (eXtensible Markup Language) is a versatile text-based markup language designed for both human readability and machine parsing. It's commonly used for data storage an

Static binding (static::) implements late static binding (LSB) in PHP, allowing calling classes to be referenced in static contexts rather than defining classes. 1) The parsing process is performed at runtime, 2) Look up the call class in the inheritance relationship, 3) It may bring performance overhead.

What are the magic methods of PHP? PHP's magic methods include: 1.\_\_construct, used to initialize objects; 2.\_\_destruct, used to clean up resources; 3.\_\_call, handle non-existent method calls; 4.\_\_get, implement dynamic attribute access; 5.\_\_set, implement dynamic attribute settings. These methods are automatically called in certain situations, improving code flexibility and efficiency.
