thinkphp function: I method browse: 59286 Release Date: 2013/06/01 Category: Documentation Tutorial Keywords: functionsThe I method of thinkphp is 3.1.3 is new, if you are a previous version of 3.*, you can refer directly to the variables section of the 3.1 Quick Start Tutorial series. Overview As you can see, the I method is a new member of the thinkphp many single-letter functions, whose name comes from the English input (input), which is mainly used for more convenient and secure access to the system input variables, available anywhere, using the following format:
I (' variable ' type. Variable name ', [' Default '],[' Filter Method '])
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 determine request type get get, post or put parameters |
| Request |
Get the request parameter |
| Put |
Get put parameters |
| Session |
Get $_session parameter |
| Cookies |
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.
Using the Get variable type as an example, we illustrate the use of the next I method:
- echo I(' get.id '); //equivalent to $_get[' id ']
- echo I(' get.name '); //equivalent to $_get[' name ']
Replication code supports default values:
- echo I(' get.id ',0); //If $_get[' ID ' is not present) returns 0
- echo I(' get.name ','); //If $_get[' name ' is not present, returns an empty string
The copy code is filtered by method:
- echo I(' get.name ',' ', 'htmlspecialchars '); //Use the Htmlspecialchars method to filter the $_get[' name '] and return an empty string if it does not exist
The copy code supports direct access to the entire variable type, for example:
- I(' get. ' ); //Get the entire $_get array
Copy code in the same way, we can get the variables of post or other input types, for example:
- I(' post.name ',' ', 'htmlspecialchars '); //Use the Htmlspecialchars method to filter the $_post[' name '] and return an empty string if it does not exist
- I(' session.user_id ',0); //Get $_session[' user_id ') default is 0 if not present
- I(' cookies. ') ); //Get the entire $_cookie array
- I(' server. Request_method '); //Get $_server[' Request_method ']
The Copy code param variable type is a framework-specific method of obtaining variable access that automatically determines the current request type, for example:
- echo I(' param.id ');
Copy the code if the current request type is get, which is equivalent to $_get[' id ', or 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
Copy the code so we can get through
- echo I(' param.1 '); //Output
- echo I(' param.2 '); //Output
- echo I(' param.3 '); //Output
Copy code in fact, the Param variable type can be simplified to:
- I(' id '); //equal to I (' param.id ')
- I(' name '); //equal to I (' Param.name ')
Copy code variable filter using the I method when the variable actually after 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 var_filters parameter filtering mechanism 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);
- }
Copy the code and then configure:
- ' var_filters '= 'filter_default '
Copy code if you need to filter multiple times, you can use:
- ' var_filters '= 'filter_default,filter_exp '
The copy code Filter_exp method is a framework built-in security filtering method that prevents 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 '
The copy code also says that all acquisition variables of the I method are htmlspecialchars filtered, then:
- I(' get.name '); //equivalent to Htmlspecialchars ($_get[' name ')
Copy code Similarly, this parameter can also support multiple filters, such as:
- ' Default_filter ' = ' strip_tags,htmlspecialchars '
Copy Code
- I(' get.name '); //equivalent to Htmlspecialchars (strip_tags ($_get[' name '))
Copy code 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 copy code I method, if passed in the function name, means that the function is called to filter the variable and returns (automatically using Array_map for filtering if the variable is an array), otherwise the PHP built-in Filter_var method is invoked for filtering processing, for example:
- I(' Post.email ',',filter_validate_email);
The copy code 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 the replication code can support must be valid values in the Filter_list method (different server environments may vary) and may support:
- Int
- Boolean
- float
- Validate_regexp
- Validate_url
- Validate_email
- Validate_ip
- string
- Stripped
- Encoded
- Special_chars
- Unsafe_raw
- Email
- Url
- Number_int
- Number_float
- Magic_quotes
- Callback
Copy code 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);
Copy code once the filter parameter is set to NULL, it means no more filtering is done.
thinkphp function: I method