How exactly is PHP creating superglobal $_POST, $_GET, $_COOKIE and $_REQUEST?
Furicane’s Questions:
I’m sorry for confusing title of the question, I’ll try to clarify what the issue is.
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.
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?
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.
Thank you for any input.
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 */
}
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.
I am trying to contribute to this question with the knowledge I know.
Sending a HTTP Request with such headers can duplicate POST variable
POST /somepage.php HTTP/1.1
Host: www.domain.com
User-Agent: Mozilla/12.0
Content-Length: 31
Content-Type: application/x-www-form-urlencoded
parameter=value&testcode=value1
Also you might want to check the HttpRequest
libray of PHP. [Start here]. For POST data you can override the previous POST content using HttpRequest::setPostFields()
and set your own data for it.
HttpRequest::setPostFields(array(
"parameter" => "value"
));