thinkphp Source Learning I method

Source: Internet
Author: User
PHP new one, recently in a project, with a thinkphp, want to deep learning, deliberately learn the source of thinkphp and make notes, to record these easy to forget things, nonsense not much to say, start.

Website Description:

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 '])

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.

The official code is as follows:

function I ($name, $default = ", $filter =null, $datas =null) {
Static $_put=null;// uses static to define a statically declared class member or method to be static and can be accessed without instantiating the class directly. Static members cannot be accessed through an object (except static methods)
if (Strpos ($name, '/'))} {// specify modifier strpos () function to find the first occurrence of a string in another string, finding the position of '/' in the parameter Nam for the first time
List ($name, $type) =explode ('/', $name, 2);//explode (separator,string,limit) function meaning to scatter a string into an array based on a specific character, limit is the number of returned arrays
}elseif (C (' var_auto_string ')) {//By default cast to string //C method called thinkphp
$type = ' s ';
}

//Summary This if means to determine whether the parameter is with/
if (Strpos ($name, '. ')) {//Specify the source of the parameter//See if it is with.!
List ($method, $name) = Explode ('. ', $name, 2);
}else{//default is auto-judgment
$method = ' param ';
}
Switch (Strtolower ($method)) {//strtolower () convert all characters to lowercase use switch to locate the type of the method
Case ' get ':
$input =& $_get;
Break
Case ' post ':
$input =& $_post;
Break
Case ' put ':
if (Is_null ($_put)) {
Parse_str (file_get_contents (' Php://input '), $_put);
}
$input=$_put;
Break
Case ' param ':
Switch ($_server[' Request_method ')} {//$_server[' Request_method ' gets the requested method using the obtained method name, using Swith () to locate the type of the method, where the thought is recursive
Case ' POST ':
$input = $_post;
Break
Case ' PUT ':
if (Is_null ($_put)) {
Parse_str (file_get_contents (' Php://input '), $_put);
}
$input=$_put;
Break
Default
$input = $_get;
}
Break
Case ' path ':
$input = Array ();
if (!empty ($_server[' path_info ')) {
$depr = C (' Url_pathinfo_depr ');
$input = Explode ($depr, trim ($_server[' path_info '), $depr));
}
Break
Case ' request ':
$input =& $_request;
Break
Case ' Session ':
$input =& $_session;
Break
Case ' Cookie ':
$input =& $_cookie;
Break
Case ' server ':
$input =& $_server;
Break
Case ' globals ':
$input =& $GLOBALS;
Break
Case ' data ':
$input =& $datas;
Break
Default
return null;
}
if (' = = $name) {//Get all variables
$data = $input;
$filters = Isset ($filter)? $filter: C (' default_filter ');//Using the three-mesh operator the isset () function is generally used to detect if a variable is set, and empty () is used to determine if it is empty
if ($filters) {
if (is_string ($filters)) {
$filters = Explode (', ', $filters);
}
foreach ($filters as $filter) {
$data = Array_map_recursive ($filter, $data); //thinkphp Parameter Filtering method array_map_recursive foreach () is a cyclic function
}
}
}elseif (Isset ($input [$name])) {//Take value operation
$data = $input [$name];
$filters = Isset ($filter)? $filter: C (' default_filter ');
if ($filters) {
if (is_string ($filters)) {
if (0 = = = Strpos ($filters, '/')) {
if (1!== preg_match ($filters, (String) $data)) {//Preg_match () to match regular expressions
Support for regular validation
return Isset ($default)? $default: null;
}
}else{
$filters = Explode (', ', $filters);
}
}elseif (Is_int ($filters)) {
$filters = Array ($filters);
}

if (Is_array ($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;
}
}
}
}
}
if (!empty ($type)) {//Use switch to determine the type of the type
Switch (Strtolower ($type)) {
Case ' a ':Array
$data=(array) $data;
Break
Case ' d ':Digital
$data=(int) $data;
Break
Case ' F ':Floating point
$data=(float) $data;
Break
Case ' B ':Boolean
$data=(Boolean) $data;
Break
Case ' s '://String
Default
$data = (string) $data;
}
}
}else{//variable default value
$data = Isset ($default)? $default: null;
}
Is_array ($data) && array_walk_recursive ($data, ' think_filter ');
return $data;
}

I method is mainly used to get foreground to the background value, the method defines four parameters, only one is necessary, this parameter is the name you want to get, the others can be changed.

The above is red is to see their own code in the process of remembering some knowledge points.

Function: Secure access to input passed parameters.

Implementation ideas:

If the commit is followed by a type, the type is processed by the specified type, and if the specified type is not followed, the types are determined based on the server variable.

Filter to ensure the security of submitted data.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

The above describes the thinkphp source learning I method, including the aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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