PHP如何创建超全球$ _POST,$ _GET,$ _COOKIE和$ _REQUEST? [英] How exactly is PHP creating superglobal $_POST, $_GET, $_COOKIE and $_REQUEST?

查看:180
本文介绍了PHP如何创建超全球$ _POST,$ _GET,$ _COOKIE和$ _REQUEST?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很抱歉混淆了问题的标题,我会试着澄清问题所在。

I'm sorry for confusing title of the question, I'll try to clarify what the issue is.

我正在做Mongrel2服务器和我正在编写一个可以访问原始HTTP请求数据的PHP处理程序。因为我在Mongrel2后面有PHP,所以没有自动创建$ _POST,$ _GET,$ _COOKIE和$ _REQUEST变量。

I'm doing some work with Mongrel2 server and I'm writing a PHP handler that has access to raw HTTP request data. Because I have PHP behind Mongrel2, there is no automatic creation of $_POST, $_GET, $_COOKIE and $_REQUEST variables.

问题是 - 有没有办法我可以将原始HTTP请求发送到PHP函数(或任何东西),这些函数将生成在使用Apache + PHP时通常可用的超全局变量吗?

The question is - is there a way that I can send the raw HTTP request to a PHP function (or anything) that will produce the superglobal variables that are usually available when using Apache + PHP?

注意:我可以手动解析HTTP请求并自己创建这些变量,但我无法找到任何关于 的文档,而PHP正是通过这种HTTP解析并导入到superglobals中。如果可能的话,我想自动完成这个超全局创建过程,而不必自己解析HTTP请求。

Note: I could parse the HTTP request manually and create those variables myself, but I wasn't able to find any documentation on how exactly PHP does this HTTP parsing and importing into superglobals. If possible, I'd like to automate this process of superglobal creation without having to parse HTTP requests myself.

感谢您的任何输入。

推荐答案

创建这些变量在PHP的内部深处理解,在 main / php_variables.c 中,在 php_auto_globals_create_get()和类似的函数中。从PHP 5.4.3开始:

Creating these variables is handled deep within the guts of PHP, in main/php_variables.c, in the php_auto_globals_create_get() and similar functions. From PHP 5.4.3:

static zend_bool php_auto_globals_create_get(const char *name, uint name_len TSRMLS_DC)
{
        zval *vars;

        if (PG(variables_order) && (strchr(PG(variables_order),'G') || strchr(PG(variables_order),'g'))) {
                sapi_module.treat_data(PARSE_GET, NULL, NULL TSRMLS_CC);
                vars = PG(http_globals)[TRACK_VARS_GET];
        } else {
                ALLOC_ZVAL(vars);
                array_init(vars);
                INIT_PZVAL(vars);
                if (PG(http_globals)[TRACK_VARS_GET]) {
                        zval_ptr_dtor(&PG(http_globals)[TRACK_VARS_GET]);
                }
                PG(http_globals)[TRACK_VARS_GET] = vars;
        }

        zend_hash_update(&EG(symbol_table), name, name_len + 1, &vars, sizeof(zval *), NULL);
        Z_ADDREF_P(vars);

        return 0; /* don't rearm */
}

这最终会直接调用SAPI (例如,Apache模块/ CGI / FastCGI /等等)来获取变量。我不认为如果你处于一个奇怪的环境中,你可以改变它的工作方式,其中GET / POST / etc变量不是PHP期望的那样。

This ends up calling directly into the SAPI (e.g, Apache module / CGI / FastCGI / whatever) to fetch variables. I don't think there's any way you can alter the way this works if you're in a weird environment where GET/POST/etc variables aren't where PHP expects them to be.

这篇关于PHP如何创建超全球$ _POST,$ _GET,$ _COOKIE和$ _REQUEST?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆