[TOC]
<?php echo "get:"; var_dump($_GET); echo "cookie:"; var_dump($_COOKIE); echo "post:"; var_dump($_POST);
$_GET
变量为例,大致流程为:获取请求字符串-->获取分割符&
-->使用=
分割key和value。if (val) { /* have a value */ size_t val_len; size_t new_val_len; *val++ = '\0'; // 对key进行url解码 php_url_decode(var, strlen(var)); // 对value进行url解码 val_len = php_url_decode(val, strlen(val)); val = estrndup(val, val_len); if (sapi_module.input_filter(arg, var, &val, val_len, &new_val_len)) { php_register_variable_safe(var, val, new_val_len, &array); } efree(val); }
00
在C语言中意味着字符串的结尾,其编码为%00
。%00
转换为00
而截断了key字符串。val_len
,即字符串长度,后续注册变量时,也是使用val_len
进行内存中的操作,所以未能截断value的值%20
替换空格while (*var_name==' ') { var_name++; }
.
会转化为_
.
转换成下划线_
。/* ensure that we don't have spaces or dots in the variable name (not binary safe) */ for (p = var; *p; p++) { if (*p == ' ' || *p == '.') { *p='_'; } else if (*p == '[') { is_array = 1; ip = p; *p = 0; break; } }
[
会转换为_
.
的转换过程不同。PHP在遇到[
符号时,会认为变量为数组。后续进行数组处理时,如果未能找到与[
匹配的]
,则会将[
替换为.
。关键代码如下:ip = strchr(ip, ']'); if (!ip) { /* PHP variables cannot contain '[' in their names, so we replace the character with a '_' */ *(index_s - 1) = '_'; index_len = 0; if (index) { index_len = strlen(index); } goto plain_var; return; }