Php get/POST and other large variable generation processes

Source: Internet
Author: User
Php get/POST and other large variable generation process, read php get/POST and other large variable generation process, author: laruence (http://www.laruence.com/) · Address: large... "> <LINKhre

By laruence (http://www.laruence.com /)
· Address: http://www.laruence.com/2008/11/07/581.html
· Reprinted, please indicate the source
This article mainly discusses the generation process of PHP large variables. In addition, if you notice that the input name submitted in the form contains a bit of a number, it is automatically converted into an underscore in PHP. And you want to know why and when it happened? This article also answers this question.
First of all, it is clear that the PHP variable name cannot contain the vertex number. However, to process the Dot name in the form, PHP automatically converts the dot (.) to an underscore (_).
To understand how PHP is handled, first we need to understand the construction process of variables such as $ _ GET, $ _ POST, and $ _ COOKIE.
After each request arrives, apache sends control to the PHP module when processing the response stage, the PHP module will call php_request_startup indirectly before processing the request (the specific call sequence is send_php-> apache_php_module_main-> php_request_startup. for details about this department, see my previous article (PHP Life Cycle ), in php_request_startup:
Int
Php_request_startup (TSRMLS_D)
{
Int
Retval = SUCCESS;

# If PHP_SIGCHILD
Signal (SIGCHLD, sigchld_handler );
# Endif
If
(Php_start_sapi () = FAILURE)
{
Return
FAILURE;
}
Php_output_activate (TSRMLS_C );
Sapi_activate (TSRMLS_C );
Php_hash_environment (TSRMLS_C );

Zend_try
{
PG (during_request_startup) = 1;
Php_output_activate (TSRMLS_C );
If
(PG (expose_php ))
{
Sapi_add_header (SAPI_PHP_VERSION_HEADER, sizeof (SAPI_PHP_VERSION_HEADER)-1, 1 );
}
}
Zend_catch
{
Retval = FAILURE;
}
Zend_end_try ();

Return
Retval;
}


Note that the php_hash_environment (TSRMLS_C) function is called. This function is the function that initializes the request-related variables before Request processing.
This function is defined in main/php_variables.c. if you are interested, see:
Int
Php_hash_environment (TSRMLS_D)
{
Char * p;
Unsigned
Char
_ Gpc_flags [5] = {0, 0, 0 };
Zend_bool
Jit_initialization = (PG (auto_globals_jit )&&! PG (register_globals )&&! PG (register_long_arrays ));
Struct
Auto_global_record
{
Char * name;
Uint
Name_len;
Char * long_name;
Uint
Long_name_len;
Zend_bool
Jit_initialization;
}
Auto_global_records [] = {
{
"_ POST", sizeof ("_ POST"), "HTTP_POST_VARS", sizeof ("HTTP_POST_VARS"), 0
},
{
"_ GET", sizeof ("_ GET"), "HTTP_GET_VARS", sizeof ("HTTP_GET_VARS"), 0
},
{
"_ COOKIE", sizeof ("_ COOKIE"), "HTTP_COOKIE_VARS", sizeof ("HTTP_COOKIE_VARS"), 0
},
{
"_ SERVER", sizeof ("_ SERVER"), "HTTP_SERVER_VARS", sizeof ("HTTP_SERVER_VARS"), 1
},
{
"_ ENV", sizeof ("_ ENV"), "HTTP_ENV_VARS", sizeof ("HTTP_ENV_VARS"), 1
},
{
"_ FILES", sizeof ("_ FILES"), "HTTP_POST_FILES", sizeof ("HTTP_POST_FILES"), 0
},
};
Size_t
Num_track_vars = sizeof (auto_global_records)/sizeof (struct
Auto_global_record );
Size_t
I;

/* Jit_initialization = 0 ;*/
For
(I = 0; I {
PG (http_globals) [I] = NULL;
}
For
(P = PG (variables_order); p & * p; p ++)
{
Switch (* p)
{
Case
'P ':
Case
'P ':
If
(! _ Gpc_flags [0] &! SG (headers_sent) & SG (request_info). request_method &&! Strcasecmp (SG (request_info). request_method, "POST "))
{
Sapi_module.treat_data (PARSE_POST, NULL, NULL
TSRMLS_CC);/* POST Data */
_ Gpc_flags [0] = 1;
If
(PG (register_globals ))
{
Php_autoglobal_merge (& EG (symbol_table), Z_ARRVAL_P (PG (http_globals) [TRACK_VARS_POST])
TSRMLS_CC );
}
}
Break;
Www.devdao.com is omitted below:
}}}


In php. ini, you can use variables_order to control whether PHP generates a large variable.
About the order, that is, if auto_register_globals is enabled, if p is processed and g is processed, then $ _ GET ['A'], it will overwrite $ _ POST ['A'];
As you can see, this is not far from success. sapi_module.treat_data is also php_default_treat_data,
In php_default_treat_data, all variables call php_register_variable_safe to register the variables. php_register_variable_safe will eventually call php_register_variable_ex:
PHPAPI
Void
Php_register_variable_ex (char * var, zval * val, zval * track_vars_array
TSRMLS_DC)
{
Char * p = NULL;
Char * ip;/* index pointer */
Char * index, * escaped_index = NULL;
Int
Var_len, index_len;
Zval * gpc_element, ** gpc_element_p;
Zend_bool
Is_array = 0;
HashTable * symtable1 = NULL;

Assert (var! = NULL );

If
(Track_vars_array)
{
Symtable1 = Z_ARRVAL_P (track_vars_array );
}
Else
If
(PG (register_globals ))
{
Symtable1 = EG (active_symbol_table );
}
If
(! Symtable1)
{
/* Nothing to do */
Zval_dtor (val );
Return;
}
/*
* Prepare variable name
*/
/* Ignore leading spaces in the variable name */
While
(* Var & * var ='
')
{
Var ++;
}
/* Ensure that we don't have spaces or dots in the variable name (not binary safe )*/
// Pay special attention to the following section ....
For
(P = var; * p; p ++)
{
If
(* P ='
'| * P = '.')
{
* P = '_';
}
Else
If
(* P = '[')
{
Is_array = 1;
Ip = p;
* P = 0;
Break;
}
... The following is omitted

The cause of the problem is found. in php_register_variable, (.) is converted (_).
Finally, we will introduce the generation of the $ _ REQUEST variable, which is actually very simple. at the end of php_hash_environment, php_auto_globals_create_request ("_ REQUEST", sizeof ("_ REQUEST")-1TSRMLS_CC) will be called) to register the _ REQUEST big variable. in php_auto_globals_create_request ("_ REQUEST", sizeof ("_ REQUEST")-1 TSRMLS_CC), simply add $ _ GET, $ _ POST, $ _ COOKIEmerge (G (http_globals) [TRACK_VARS_COOKIE, for more information, see my previous study on getting $ _ GET/$ _ POST/$ _ COOKIE in the PHP Module ):
Static
Zend_bool
Php_auto_globals_create_request (char * name, uint
Name_len
TSRMLS_DC)
{
Zval * form_variables;
Unsigned
Char
_ Gpc_flags [3] = {0, 0 };
Char * p;

ALLOC_ZVAL (form_variables );
Array_init (form_variables );
INIT_PZVAL (form_variables );

For
(P = PG (variables_order); p & * p; p ++)
{
Switch
(* P)
{
Case 'g ':
Case 'g ':
If
(! _ Gpc_flags [0])
{
Php_autoglobal_merge (Z_ARRVAL_P (form_variables), Z_ARRVAL_P (PG (http_globals) [TRACK_VARS_GET])
TSRMLS_CC );
_ Gpc_flags [0] = 1;
}
Break; case 'P ':
Case 'P ':
If
(! _ Gpc_flags [1])
{
Php_autoglobal_merge (Z_ARRVAL_P (form_variables), Z_ARRVAL_P (PG (http_globals) [TRACK_VARS_POST])
TSRMLS_CC );
_ Gpc_flags [1] = 1;
}
Break;
Case 'C ':
Case 'C': if
(! _ Gpc_flags [2])
{
Php_autoglobal_merge (Z_ARRVAL_P (form_variables), Z_ARRVAL_P (PG (http_globals) [TRACK_VARS_COOKIE])
TSRMLS_CC );
_ Gpc_flags [2] = 1;
}
Break;
}
}
Zend_hash_update (& EG (symbol_table), "_ REQUEST", sizeof ("_ REQUEST"), & form_variables, sizeof (zval *), NULL );
Return
0;
}

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.