<?php
/*
*thinkphp3.2 added an I method to take the semantic input to get the data from the foreground submission.
* Here are some tips for using the following I () method:
The *i method was placed in the/thinkphp/common/function.php.
*/
/**
* Get input parameter support filter and default value
* Use method:
* <code>
* I (' id ', 0); Get the ID parameter to automatically determine the getting or post
* I (' post.name ', ', ', ' htmlspecialchars '); Get $_post[' name '
* I (' get. '); Get $_get
* </code>
* @param string $name variable name supports the specified type
* @param the default value when the mixed $default does not exist
* @param mixed $filter parameter filtration method
* @return Mixed
*/
function I ($name, $default = ', $filter =null) {
if (Strpos ($name, '. ')) {//Specify parameter origin
Determine whether the. Number is included in the parameter $name.
List ($method, $name) = Explode ('. ', $name, 2);
If the. Number is included, the. Number is separated and assigned to $method and $name respectively.
}else{//default for automatic judgment
If there is no. No.
$method = ' param ';
}
Switch (Strtolower ($method)) {//convert $method to lowercase
If $method is get, then $input is $_get
Case ' get ': $input =& $_get;break;
If $method is get, then $input is $_post
Case ' post ': $input =& $_post;break;
If put, the original data parameter of post is given to $input
Case ' put ': Parse_str (file_get_contents (' Php://input '), $input);
If it's Param,
Case ' param ':
Judge $_server[' Request_method ']
Switch ($_server[' Request_method ']) {
If it is post, the content of the $input is $_post content
Case ' POST ':
$input = $_post;
Break
If put. Then the contents of input are put
Case ' put ':
Parse_str (file_get_contents (' Php://input '), $input);
Break
The default is $_get content
Default
$input = $_get;
}
Break
If $method is REQUEST, then $input is $_request
Case ' request ': $input =& $_request; Break
If $method is session, $input is $_session
Case ' Session ': $input =& $_session; Break
If $method is a COOKIE, $input is $_cookie
Case ' cookie ': $input =& $_cookie; Break
If $method is a SERVER, $input is $_server
Case ' server ': $input =& $_server; Break
If $method is GLOBALS, then $input is $globals
Case ' globals ': $input =& $GLOBALS; Break
return NULL by default
Default
return NULL;
}
/**
* So far, all the variables have been taken, based on the needs of the incoming parameters (front of the first parameter. Number). The following is the contents of the return variable.
**/
If the $name is empty, that is, the. Number of the first parameter of I () is empty after the
if (empty ($name)) {//Get all variables
Gets the variable $input all copied to $data
$data = $input;
array_walk_recursive-user functions recursively in each member of an array
Takes the key value of the $data as the first parameter of the FILTER_EXP function, and the key name as the second argument
If the key value of the $data contains either the or exp characters, it automatically adds a space at the back
Array_walk_recursive ($data, ' filter_exp ');
Determine if there are filter parameters, if any, directly use the filtering method, if not, use the filtering method in the configuration
$filters = Isset ($filter) $filter: C (' default_filter ');
if ($filters) {
$filters = Explode (', ', $filters);
Apply each method in the filter parameter to the $data
foreach ($filters as $filter) {
Use $filters filter for each value of $data
$data = Array_map_recursive ($filter, $data); Parameter filtering
}
}
}elseif (Isset ($input [$name])) {//value operation
$data = $input [$name];
Is_array ($data) && array_walk_recursive ($data, ' filter_exp ');
$filters = Isset ($filter) $filter: C (' default_filter ');
if ($filters) {
$filters = Explode (', ', $filters);
foreach ($filters as $filter) {
if (function_exists ($filter)) {
$data = Is_array ($data) array_map_recursive ($filter, $data): $filter ($data); Parameter filtering
}else{
$data = Filter_var ($data, Is_int ($filter) $filter: filter_id ($filter));
if (false = = $data) {
return Isset ($default)? $default: NULL;
}
}
}
}
}else{//variable default value
$data = Isset ($default)? $default: NULL;
}
return $data;
}
?>