thinkphp I method use detailed _php tutorial

Source: Internet
Author: User
The I method of thinkphp is a new member of many single-letter functions, its name is 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 '])
The type of the variable refers to the request or input type.

Each variable type has the following meanings:

Variable Type meaning
get Get the Get parameter
post get post parameters
param auto-judge request type get get, post or put parameter
request Get the request parameter
put Get the put parameter
session get $_session parameter
cookie get $_cookie parameter
server get $_server parameter
Globals Get $GLOBALS parameters


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

1. Usage:

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:

echo I (' get.name ', ' ', ' htmlspecialchars '); $_get[' name '] is filtered using the Htmlspecialchars method, and an empty string is returned if it does not exist

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

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

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.
And the param type variable can also get the URL parameter in the form of a numeric index (must be valid for the PathInfo mode parameter, either get or post), for example:
The current Access URL address is
Http://serverName/index.php/New/2013/06/01

Then we can pass

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

In fact, the Param variable type can be simplified to:

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

2. Variable filtering

When using the I method, the variable actually passes through two filters, the first is the global filter, the global filter is configured by the Var_filters parameter, it is important to note that after the 3.1 version, the filtering mechanism of the Var_filters parameter has been changed to adopt Array_walk_ Recursive method recursive filtering, the main requirement for the filtering method is that the reference must be returned, so setting htmlspecialchars here is not valid, you can customize a method, for example:

Function Filter_default (& $value) {  $value = Htmlspecialchars ($value);}

Then configure:

' Var_filters ' = ' filter_default '

If you need to filter multiple times, you can use:

' Var_filters ' = ' filter_default,filter_exp '

The Filter_exp method is a security filtering method built into the framework to prevent injection attacks using the EXP function of the model.

Because the Var_filters parameter is set by the global filtering mechanism, and the use of recursive filtering, has an impact on efficiency, so, we recommend directly to get the variable filter, in addition to the I method of the third parameter to set the filter method, you can also use the configuration Default_ Filter parameter, in fact, the default setting for this parameter is:

' default_filter '    = ' htmlspecialchars '

It is also said that all acquisition variables of the I method are htmlspecialchars filtered, then:

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

Similarly, this parameter can support multiple filters, such as:

' default_filter '    = ' strip_tags,htmlspecialchars '

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

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

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

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

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

Indicates that $_post[' email ' is formatted and returns an empty string if it does not meet the requirements.
(For more verification formats, refer to the Filter_var usage of the official manual.) )
Or you can use the following characters to identify the way:

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

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

int Boolean floatvalidate_regexpvalidate_urlvalidate_emailvalidate_ip Stringstrippedencodedspecial_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:

I (' Get.name ', ', NULL);

Once the filter parameter is set to NULL, any filtering is no longer performed.


http://www.bkjia.com/PHPjc/825333.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/825333. The I method of htmltecharticlethinkphp is a new member of many single-letter functions, its name is from the English input (input), mainly used for more convenient and secure access to the system input variables, can be used in any ...

  • 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.