Usage Analysis of filter_input Function in php

  • 2021-08-03 09:47:31
  • OfStack

In this paper, the usage of filter_input function in php is analyzed with examples. Share it for your reference. The specific analysis is as follows:

In php5.2, filter module is built in, which is used for variable verification and filtering, filtering variables and other operations. Here we look at how to directly filter the content input by users.

The filter_input function corresponding to the fliter module is very simple to use. For example, if we filter the get parameter named sample input by the user as an integer, we can write as follows:

filter_input(input_get, "sample", filter_sanitize_number_int);

The parameters of filter_input are the user input type, the corresponding input name, and the filter (validation) constant. At present, filter_input supports the following user inputs:

input_get //  Correspondence  $_get 
input_post // Correspondence $_post
input_cookie // Correspondence $_cookie
input_server // Correspondence $_server
input_env // Correspondence $_env

With the built-in verification tags, similar "physical work" such as user input filtering can be solved. Finally, it is necessary to mention a small trap of filter.
filter_var('abc', filter_validate_boolean); // bool(false) 
filter_var('0',   filter_validate_boolean); // bool(false)

fliter module is mentioned again on php arch. It is true that this module can save us a lot of time. Here, if the data provided by users such as $_ get and $_ post are used improperly, such as incomplete verification and filtering, it is easy to cause security problems. Usually, we will write "1 lump" regularity to verify whether the data format is legal.

I hope this article is helpful to everyone's PHP programming.


Related articles: