编程语言
首页 > 编程语言> > php $_SERVER

php $_SERVER

作者:互联网

PHP RINIT STATE HOW TO GET REQ IP

背景

void php_startup_auto_globals(void)
{
	zend_register_auto_global(zend_string_init_interned("_GET", sizeof("_GET")-1, 1), 0, php_auto_globals_create_get);
	zend_register_auto_global(zend_string_init_interned("_POST", sizeof("_POST")-1, 1), 0, php_auto_globals_create_post);
	zend_register_auto_global(zend_string_init_interned("_COOKIE", sizeof("_COOKIE")-1, 1), 0, php_auto_globals_create_cookie);
	zend_register_auto_global(zend_string_init_interned("_SERVER", sizeof("_SERVER")-1, 1), PG(auto_globals_jit), php_auto_globals_create_server);
	zend_register_auto_global(zend_string_init_interned("_ENV", sizeof("_ENV")-1, 1), PG(auto_globals_jit), php_auto_globals_create_env);
	zend_register_auto_global(zend_string_init_interned("_REQUEST", sizeof("_REQUEST")-1, 1), PG(auto_globals_jit), php_auto_globals_create_request);
	zend_register_auto_global(zend_string_init_interned("_FILES", sizeof("_FILES")-1, 1), 0, php_auto_globals_create_files);
}

$_SERVER $_ENV $REQUEST 变量为即使变量(Just IN Time)
They are created when first used(just in time).

参考的方案

1、 $_SERVER 变量中去获取 IP
存在问题 $_SERVER 并没有初始化(RINIT)

2、http_globals 全局变量获取
参考鸟哥的博客

3、http_globals 全局变量解析

解决方案

PHP_RINIT(module) 阶段

	// Server 变量是延迟创建 just  in time
	if (PG(auto_globals_jit)) {
		//if (php_auto_globals_create_server(server)) {
		if (zend_is_auto_global_str(ZEND_STRL("_SERVER")) {
			DEBUG("Register TRACK_VARS_SERVER Success\n");
		}
	}
	zval *server, *field;

	zend_string *ip = NULL;

	server = &PG(http_globals)[TRACK_VARS_SERVER];

	if ( server != NULL && Z_TYPE_P(server) == IS_ARRAY) {
		//Get Client Real IP
		if ((field = zend_hash_str_find(Z_ARRVAL_P(server), "HTTP_CLIENT_IP", sizeof("HTTP_CLIENT_IP") - 1)) &&

			Z_TYPE_P(field) == IS_STRING) {
			ip = Z_STR_P(field);

		}
		else if ((field = zend_hash_str_find(Z_ARRVAL_P(server), "HTTP_X_FORWARDED_FOR", sizeof("HTTP_X_FORWARDED_FOR") - 1)) &&

			Z_TYPE_P(field) == IS_STRING) {
			ip = Z_STR_P(field);

		}
		else if ((field = zend_hash_str_find(Z_ARRVAL_P(server), "REMOTE_ADDR", sizeof("REMOTE_ADDR") - 1)) &&

			Z_TYPE_P(field) == IS_STRING) {
			ip = Z_STR_P(field);

		}
		else {
			zend_error(512, "Get Request Info About IP Failed");
			
			//return;
		}

标签:auto,zend,SERVER,server,globals,sizeof,php
来源: https://blog.csdn.net/qq_41630483/article/details/120328732