PHP Filter Library (extension related to variables and types), can filter common mail, IP, variable array, etc.

Source: Internet
Author: User
Tags what php
PHP Filter Library (extensions related to variables and types) to filter common mail, IP, array of variables, etc.

?

Introduction to the Filter extension library

?

This extension filters data by either validating or sanitizing it. This is especially useful if the data source contains unknown (or foreign) data, like user supplied input. For example, the this data is come from an HTML form.

There is both main types of filtering: validation and sanitization.

Validation is used to validate or check if the data meets certain qualifications. For example, passing FILTER_VALIDATE_EMAIL in would determine if the data is a valid e-mail address, but won't change the data i Tself.

Sanitization would sanitize the data, so it could alter it by removing undesired characters. For example, passing FILTER_SANITIZE_EMAIL in would remove characters that is inappropriate for a email address to contain. That's said, it does not validate the data.

Flags is optionally used with both validation and sanitization to tweak behaviour according to need. For example, passing FILTER_FLAG_PATH_REQUIRED in and filtering an URL would require a path (like /foo in Http://exampl E.org/foo) to be present.

?

This extended filter data is validated and disinfected. This is especially useful when the data source contains unknown (or foreign) data, such as user-supplied input.  For example, this data might come from an HTML form.  There are two main types of filtering: validation and data disinfection. Validation is used to verify or check that the data meets certain criteria.  For example, by verifying the email in the filter, the data is determined to be a valid e-mail address but does not alter the data itself. The data will be clean and hygienic, so it may change it by eliminating unwelcome characters. For example, deleting a character by filtering in the filter will not contain an e-mail address.  It is said that it does not validate data. Flags are selectively used for validation and data disinfection as needed to adjust behavior. For example, Passin

?

List of filtered functions:

Functions of function names

Filter_list Filter List--Returns all supported filters for a list

FILTER_ID Filter ID-The return filter ID belongs to a filter called

Filter_has_var Filter already var-check if the variable specifies the type of existence

Filter_input Filter Input--Get the name of a specific external variable and selectively filter it

Filter_input_array Filter Input arrays-Get external variables and selectively filter them

Filter_var filters specified using the Filter_var filter variable

Filter_var_array Filter var array--To get multiple variables and selectively filter them

?

?

?

?

Examples of functions:

Filter_list

?

 
  The output of the above routines is similar to the following: Array (    [0] = + int    [1] = = Boolean    [2] = = = Float    [3]    = = Validate_regexp [4] = > Validate_url    [5] = Validate_email    [6] = Validate_ip    [7] = = String    [8] = = stripped< C11/>[9] = encoded    [ten] = Special_chars    [one] = Unsafe_raw    [] = email    [+] = URL    [+] = Number_int    [[]] = Number_float    [+] = magic_quotes    [+] = callback)

?

? filter_list and FILTER_ID combined

?

 
  ";p Rint_r (Filter_list ()); echo"
"; foreach (Filter_list () as $key = = $value) {echo"
". $key." = ". $value. ' = '. filter_id ($value);}? >0=int=2571=boolean=2582=float=2593=validate_regexp=2724=validate_url=2735=validate_email=2746=validate_ip= 2757=string=5138=stripped=5139=encoded=51410=special_chars=51511=unsafe_raw=51612=email=51713=url=51814=number _int=51915=number_float=52016=magic_quotes=52117=callback=1024

?

? filter_id

?

 
  

?

?

Filter_has_var function Efficiency:

To Note:filter_has_var () is a bit faster than isset ()

Translation: Filter_has_var function faster than isset ()

?

Please note that the function does not check the live array, it actually checks the content received by php:

Translation: Please note that this function does not check the include array, it only checks what PHP receives

?

?

 
  Would say "No", unless the parameter is actually in the querystring. Also, if the input var is empty, it'll say Yes.

?

?

?

?

Verify Example 1 (Verify mailbox):

?

 
  

? Above program output:

?

This (email_a) e-mail address is considered valid.

?

Verification Example 2 (authentication IP)

?

 
  

? Above program output:

?

This (ip_a) IP address is considered valid.

?

Validation Example 3 (filter variables by option):

?

 
  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 (bet Ween 0 and 3). \ n ";} if (Filter_var ($int _c, Filter_validate_int, $options)!== FALSE) {    echo ' this (int_c) integer is considered valid (bet Ween 0 and 3). \ n ";} $options [' Options '] [' default '] = 1;if (($int _c = Filter_var ($int _c, Filter_validate_int, $options))!== FALSE) {    

? The above program will output:

?

This (int_a) integer is considered valid (between 0 and 3). This (int_c) integer was considered valid (between 0 and 3) and is 1.

?

Disappear harmful characters and verify example 1:

?

 
  

? The above program will output:

?

This (a) sanitized e-mail address is considered valid. This (b) sanitized e-mail address is considered invalid. This (c) sanitized e-mail address is considered valid. Before: ([email protected]) after: [Email protected]

?

Here is a brief introduction of Filter_input, from Baidu Encyclopedia:

?

Definition and usage

The Filter_input () function takes input from outside the script and filters it.

This function is used to validate variables from non-secure sources, such as the user's input.

This function can get input from various sources:

Input_get

Input_post

Input_cookie

Input_env

Input_server

Input_session (not yet implemented)

Input_request (not yet implemented)

If successful, the filtered data is returned, if it fails, false if? variable The parameter is not set, then NULL is returned.

Grammar

Filter_input (input_type, variable, filter, options)

Parameter description
Input_type Necessary. Specifies the type of input. See the possible types in the list above.
Variable Specifies the variables to filter.
Filter Optional. Specifies the ID of the filter to be used. The default is filter_sanitize_string.?
See the full PHP Filter function reference manual for possible filters.
The filter ID can be an ID name (such as Filter_validate_email), or an ID number (such as 274).
Options Specifies an array that contains flags/options. Check the possible flags and options for each filter.

Example

In this example, we use the Filter_input () function to filter a POST variable. The accepted POST variable is a valid e-mail address.

  

?

{echo "e-mail is not valid";}  else {echo "e-mail is valid";} ?> output similar to: e-mail is valid
?

Filter and verify the type of string for details see the official PHP manual

When I wrote this, I found a friend who had written it before, and gave a link to see more of it. http://blog.johnsonlu.org/phpfilter/

  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.