Read global variables such as $ _ Get \ $ _ post in PHP Kernel

Source: Internet
Author: User

1. Get global variables such as $ _ Get \ $ _ post \ $ _ Server \ $ _ files \ $ _ cookie in the kernel
Hashtable is the basis for many PHP implementations, such as $ _ Get \ $ _ post and other global variable implementations.
Then, read the global variables in the extension, and of course operate hashtable.
Initialize environment variables in the kernel through php_hash_environment (tsrmls_d) in main/php_variables.c)

When initializing environment variables, PHP registers a read-only environment variable through php_startup_auto_globals of main/php_variables.c, taking into account the reading performance of large variables.

void php_startup_auto_globals(TSRMLS_D){zend_register_auto_global("_GET", sizeof("_GET")-1, NULL TSRMLS_CC);zend_register_auto_global("_POST", sizeof("_POST")-1, NULL TSRMLS_CC);zend_register_auto_global("_COOKIE", sizeof("_COOKIE")-1, NULL TSRMLS_CC);zend_register_auto_global("_SERVER", sizeof("_SERVER")-1, php_auto_globals_create_server TSRMLS_CC);zend_register_auto_global("_ENV", sizeof("_ENV")-1, php_auto_globals_create_env TSRMLS_CC);zend_register_auto_global("_REQUEST", sizeof("_REQUEST")-1, php_auto_globals_create_request TSRMLS_CC);zend_register_auto_global("_FILES", sizeof("_FILES")-1, NULL TSRMLS_CC);}

Read-Only environment variables:

&PG(http_globals)[TRACK_VARS_GET];&PG(http_globals)[TRACK_VARS_POST];&PG(http_globals)[TRACK_VARS_REQUEST];&PG(http_globals)[TRACK_VARS_COOKIE];&PG(http_globals)[TRACK_VARS_ENV];&PG(http_globals)[TRACK_VARS_FILES];

The macros are used to provide the kernel

/*main/php_globals.h*/#define TRACK_VARS_POST0#define TRACK_VARS_GET1#define TRACK_VARS_COOKIE2#define TRACK_VARS_SERVER3#define TRACK_VARS_ENV4#define TRACK_VARS_FILES5#define TRACK_VARS_REQUEST6

If you need to modify the environment variables, you can search for them in the global scope:

(void)zend_hash_find(&EG(symbol_table), ZEND_STRS("_GET"), (void **)&carrier);(void)zend_hash_find(&EG(symbol_table), ZEND_STRS("_POST"), (void **)&carrier);(void)zend_hash_find(&EG(symbol_table), ZEND_STRS("_REQUEST"), (void **)&carrier);(void)zend_hash_find(&EG(symbol_table), ZEND_STRS("_COOKIE"), (void **)&carrier);(void)zend_hash_find(&EG(symbol_table), ZEND_STRS("_FILES"), (void **)&carrier);(void)zend_hash_find(&EG(symbol_table), ZEND_STRS("_ENV"), (void **)&carrier);

2. Now let's look at the case and define a request class, which has the getquery and getpost methods to read $ _ Get and $ _ post.

/** Fw_request.c ** created on: 2012-6-19 * Author: JY */# ifdef have_config_h # include "config. H "# endif # include" php. H "# include" php_ini.h "# include" Main/SAPI. H "# include" Zend/zend_exceptions.h "# include" Zend/zend_alloc.h "# include" php_f1_h "# include" fw_request.h "zend_class_entry * request; /*** class request {public mixed getlang (void); Public mixed getquery (string $ name = NULL);} * // defines the Class Method Parameter type. The kernel automatically verifies the parameter type token (getqueryargs, 1) zend_arg_info (0, name) zend_end_arg_info () token (getpostargs, 1) zend_arg_info (0, name) zend_end_arg_info () zend_method (request, getquery) {zval ** carrier; zval * Key, * ret; If (zend_parse_parameters (zend_num_args () tsrmls_cc, "Z", & Key) = failure) {return_false;} ret = request_query (track_vars_get, z_strval_p (key), z_strlen_p (key) tsrmls_cc ); Return_zval (Ret, 0, null);} zend_method (request, getpost) {zval ** carrier; zval * Key, * ret; If (zend_parse_parameters (zend_num_args () tsrmls_cc, "Z", & Key) = failure) {return_false;} ret = request_query (track_vars_post, z_strval_p (key), z_strlen_p (key) tsrmls_cc); return_zval (Ret, 0, null);} zend_function_entry functions_entry [] = {php_me (request, getquery, getqueryargs, zend_acc_public) php_me (request, Ge Tpost, getpostargs, zend_acc_public)}; sums (fw_request) {zend_class_entry CE; init_class_entry (CE, "request", functions_entry); Request = sums (& ce, null, null tsrmls_cc ); return success ;} /* define request_query to read $ _ Get \ $ _ post \ $ _ Server \ $ _ files \ $ _ cookie \ $ _ request in the kernel. Only the value of $ _ request is */zval * request_query (uint type, char * Name, uint Len tsrmls_dc) {zval ** carrier, ** Ret; zend_bool jit_initialization = (PG (auto_globals_jit )&&! PG (register_globals )&&! PG (Rack); Switch (type) {Case track_vars_post: Case track_vars_get: Case track_vars_files: Case track_vars_cookie: Carrier = & PG (http_globals) [type]; break; Case track_vars_env: if (jit_initialization) {constraint (zend_strl ("_ env") tsrmls_cc);} carrier = & PG (http_globals) [type]; break; Case track_vars_server: If (jit_initialization) {zend_is_auto_global (zend_strl ("_ server") tsrmls_cc) ;} Carrier = & PG (http_globals) [type]; break; Case track_vars_request: If (jit_initialization) {zend_is_auto_global (zend_strl ("_ request") tsrmls_cc) zend_hash_find (& EG (symbol_table), zend_strs ("_ request"), (void **) & carrier); break; default: break;} If (! Carrier |! (* Carrier) {zval * empty; make_std_zval (empty); zval_null (empty); return empty;} If (! Len) {z_addref_p (* carrier); return * carrier;} If (zend_hash_find (z_arrval_pp (carrier), name, Len + 1, (void **) & RET) = failure) {zval * empty; make_std_zval (empty); zval_null (empty); return empty;} z_addref_p (* RET); return * ret ;}

Iii. Web page access PHP Testing

Http: // localhost/test. php? Get = test. This URL is determined by your local environment.

$_GET['test'] = "abc";$obj = new request();echo $obj->getQuery('test')."<br>";echo $obj->getPost('test');

Output result:
Test
Null

This also proves that request_query reads read-only global variables.

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.