thinkphp built-in function----I method

Source: Internet
Author: User

Although you can still use the traditional way to obtain various system variables during the development process, for example:

$id = $_get[' id '];  Get get variable $name = $_post[' name ']; Get post variable $value = $_session[' var ']; Get Session variable $name = $_cookie[' name ']; Get cookie variable $file = $_server[' php_self ']; Get Server variables

However, we do not recommend the direct use of traditional methods to obtain, because there is no unified security processing mechanism, later if the adjustment, the change will be more troublesome. Therefore, a better approach is to use the I function uniformly in the framework for variable acquisition and filtering.

The I method is a new member of the thinkphp many single-letter functions, named from the English input (input), mainly for more convenient and secure access to the system input variables, can be used anywhere, the usage format is as follows:

I (' Variable type ', ' variable name ', [' Default '],[' Filter method '],[' extra data source '])

The type of the variable refers to the type of request or input, including:

variable type meaning
get get get Parameters
Post get post parameters
param automatically determines request type get get, post, or put parameter
Request GET request parameter
put get put parameter
session get $_session parameter
Cookie get $_cookie parameter
server get $_server parameter
Globals get $GLOBALS parameters
path get URL parameter for pathinfo mode (3.2.2 New)
data gets other types of parameters that need to be combined with additional data source parameters (3.2.2 New)

Note: Variable types are not case-sensitive.
Variable names are strictly case-sensitive.
Both the default and filtering methods are optional parameters.

We take the get variable type as an example to illustrate the use of the next I method:

echo I (' get.id '); Equivalent to $_get[' id ']echo I (' get.name '); Equivalent to $_get[' name ')

Default values are supported:

echo I (' Get.id ', 0); Returns 0echo I (' get.name ', ') if there is no $_get[' id '); Returns an empty string if $_get[' name ' is not present

Filter by Method:

Use the Htmlspecialchars method to filter the $_get[' name '] and return an empty string if it does not exist echo I (' get.name ', ' ', ' htmlspecialchars ');

Supports direct access to the entire variable type, for example:

Gets the entire $_get array I (' get. ');

In the same way, we can get the variables of post or other input types, for example:

I (' post.name ', ' ', ' htmlspecialchars '); The Htmlspecialchars method is used to filter the $_post[' name '] and returns an empty string I (' session.user_id ', 0) if it does not exist; Get $_session[' user_id ' if not present then the default is 0I (' cookie. '); Gets the entire $_cookie array I (' Server. Request_method '); Get $_server[' Request_method ']

The Param variable type is a framework-specific method of obtaining a variable that automatically determines the current request type, for example:

echo I(‘param.id‘);

If the current request type is get, then it is equivalent to $_get[' ID '), if the current request type is POST or put, then it is equivalent to getting $_post[' id ' or put parameter ID.

Because the Param type is the variable type that is obtained by default for the I function, the fact that the Param variable type can be simplified to:

I (' id '); Equivalent to I (' param.id ') I (' name '); Equivalent to I (' Param.name ')

3.2.2 has been added path和data两个变量类型 with the following usage:

The path type variable can be used to get the URL parameter (the pathinfo mode parameter must be valid, either get or post), for example: the current Access URL address ishttp://serverName/index.php/New/2013/06/01

Then we can pass

echo I (' path.1 '); Output 2013echo I (' path.2 '); Output 06echo I (' path.3 '); Output 01

The data type variable can be used to get a read of an unsupported variable type, for example:

I (' Data.file1 ', ', ', ', $_files);
Variable filtering

If you do not specify a filtering method when invoking the I function, the system will use the default filtering mechanism (configured by Default_filter), in fact, the default setting for this parameter is:

System default variable filtering mechanism ' default_filter ' = ' htmlspecialchars '

It is also said that all the acquired variables of the I method will be htmlspecialchars filtered if no filtering method is set, then:

Equivalent to Htmlspecialchars ($_get[' name ') ' I (' get.name ');

Similarly, this parameter can also be set to support multiple filters, for example:

' Default_filter ' = ' strip_tags,htmlspecialchars '

After setting up, we are using:

Equivalent to Htmlspecialchars (strip_tags ($_get[' name ')) I (' Get.name ');

If we specify a filtering method when using the I method, the Default_filter setting is ignored, for example:

Equivalent to Strip_tags ($_get[' name ')) echo I (' get.name ', ' ', ' strip_tags ');

The third parameter of the I method, if passed in the function name, means that the function is called to filter the variable and returns (automatically used for filtering if the variable is an array array_map ), otherwise the PHP built-in method is called for filter_var filtering processing, for example:

I (' Post.email ', ', filter_validate_email);

Indicates that the $_POST[‘email‘] format is validated and an empty string is returned if the requirement is not met. (For more verification formats, you can refer to the usage of the official manual filter_var .) ) or you can use the following characters to identify the way:

I (' Post.email ', ' ', ' email ');

The filter names that can be supported must be filter_list valid values in the method (different server environments may vary) and may support the following:

Intbooleanfloatvalidate_regexpvalidate_urlvalidate_emailvalidate_ipstringstrippedencodedspecial_charsunsafe_ Rawemailurlnumber_intnumber_floatmagic_quotescallback

In some special cases, we do not want to do any filtering, even if the default_filter has been set, you can use:

The following two methods do not adopt any filtering method I (' get.name ', ' ', '); I (' Get.id ', ", false);

Once the filter parameter is set to an empty string or false, any filtering is no longer performed.

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.