<?php
declare(strict_types=1);
$cases = [];
// Case 1: POST request keeps clean URL and parses form body into $_POST
(function () use (&$cases): void {
$targetUrl = 'https://example.org/welcome.php';
$rawBody = http_build_query(['fname' => '林青', 'age' => '25']);
$post = [];
parse_str($rawBody, $post);
$actual = [
'url_has_query' => parse_url($targetUrl, PHP_URL_QUERY) !== null,
'post_keys' => array_keys($post),
'fname' => $post['fname'],
'age' => (int)$post['age'],
'greeting' => '欢迎 ' . htmlspecialchars((string)$post['fname'], ENT_QUOTES, 'UTF-8') . '!你的年龄是 ' . (int)$post['age'] . ' 岁。',
];
$expected = [
'url_has_query' => false,
'post_keys' => ['fname', 'age'],
'fname' => '林青',
'age' => 25,
'greeting' => '欢迎 林青!你的年龄是 25 岁。',
];
$cases[] = [
'name' => 'post_body_parse_and_clean_url',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
// Case 2: Inspecting PHP post_max_size default (8M = 8388608 bytes)
(function () use (&$cases): void {
$rawLimit = (string)ini_get('post_max_size');
$toBytes = static function (string $val): int {
$unit = strtolower(substr($val, -1));
$num = (int)$val;
return match ($unit) {
'g' => $num * 1024 * 1024 * 1024,
'm' => $num * 1024 * 1024,
'k' => $num * 1024,
default => $num,
};
};
$limitBytes = $toBytes($rawLimit);
$actual = [
'post_max_size_ini' => $rawLimit,
'limit_bytes' => $limitBytes,
'payload_1mb_allowed' => (1024 * 1024) <= $limitBytes,
'payload_10mb_allowed' => (10 * 1024 * 1024) <= $limitBytes,
];
$expected = [
'post_max_size_ini' => '8M',
'limit_bytes' => 8388608,
'payload_1mb_allowed' => true,
'payload_10mb_allowed' => false,
];
$cases[] = [
'name' => 'post_max_size_ini_check',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
// Case 3: $_REQUEST merge behavior (GET + POST + COOKIE order) vs explicit superglobals
(function () use (&$cases): void {
$get = ['fname' => 'FromGet', 'age' => '18', 'source' => 'url'];
$post = ['fname' => 'FromPost', 'age' => '25'];
$cookie = ['session_theme' => 'light'];
// Standard GP / GPC order: POST overrides GET when keys collide
$requestGP = array_merge($get, $post);
$requestGPC = array_merge($get, $post, $cookie);
$actual = [
'request_gp_fname' => $requestGP['fname'],
'request_gp_source' => $requestGP['source'],
'request_gpc_keys' => array_keys($requestGPC),
'explicit_get_fname' => $get['fname'],
'explicit_post_fname' => $post['fname'],
];
$expected = [
'request_gp_fname' => 'FromPost',
'request_gp_source' => 'url',
'request_gpc_keys' => ['fname', 'age', 'source', 'session_theme'],
'explicit_get_fname' => 'FromGet',
'explicit_post_fname' => 'FromPost',
];
$cases[] = [
'name' => 'request_superglobal_precedence',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
echo json_encode(['version' => PHP_VERSION, 'cases' => $cases], JSON_UNESCAPED_UNICODE | JSON_UNESCAPED_SLASHES) . "\n";
本例需在 Windows x64、PHP 8.4 本地 CLI 环境。 中运行,请复制源码、保存文件或下载工程包后在本地执行验证。
运行说明与环境独立工程
运行环境:Windows x64、PHP 8.4 本地 CLI 环境。
入口文件:projects/php-post-lab/explore-post.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-post.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1282.zip