<?php
declare(strict_types=1);
$cases = [];
// Case 1: Basic POST form fields extraction and safe escaping
(function () use (&$cases): void {
$rawBody = 'fname=' . rawurlencode('林青') . '&age=24';
$post = [];
parse_str($rawBody, $post);
$fname = htmlspecialchars((string)($post['fname'] ?? ''), ENT_QUOTES, 'UTF-8');
$age = (int)($post['age'] ?? 0);
$rendered = "欢迎 {$fname}!你的年龄是 {$age} 岁。";
$actual = [
'fname' => $fname,
'age' => $age,
'rendered' => $rendered,
];
$expected = [
'fname' => '林青',
'age' => 24,
'rendered' => '欢迎 林青!你的年龄是 24 岁。',
];
$cases[] = [
'name' => 'form_post_fields_extract',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
// Case 2: Single-select dropdown and radio button via GET
(function () use (&$cases): void {
$catalog = [
'PHP' => 'PHP 官方文档|https://www.php.net',
'MDN' => 'MDN Web Docs|https://developer.mozilla.org',
'W3C' => 'W3C 标准|https://www.w3.org',
];
$resolveSingle = static function (string $query) use ($catalog): array {
$get = [];
parse_str($query, $get);
$q = isset($get['q']) ? htmlspecialchars((string)$get['q'], ENT_QUOTES, 'UTF-8') : '';
if ($q !== '' && isset($catalog[$q])) {
return ['mode' => 'result', 'value' => $q, 'output' => $catalog[$q]];
}
return ['mode' => 'form', 'value' => $q, 'output' => ''];
};
$actual = [
'select_php' => $resolveSingle('q=PHP'),
'radio_mdn' => $resolveSingle('q=MDN'),
'empty_default' => $resolveSingle(''),
];
$expected = [
'select_php' => ['mode' => 'result', 'value' => 'PHP', 'output' => 'PHP 官方文档|https://www.php.net'],
'radio_mdn' => ['mode' => 'result', 'value' => 'MDN', 'output' => 'MDN Web Docs|https://developer.mozilla.org'],
'empty_default' => ['mode' => 'form', 'value' => '', 'output' => ''],
];
$cases[] = [
'name' => 'select_single_and_radio_get',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
// Case 3: Multi-select dropdown (q[]) and checkbox group (q[]) via POST array
(function () use (&$cases): void {
$catalog = [
'HTML' => 'HTML5: https://html.spec.whatwg.org',
'CSS' => 'CSS: https://www.w3.org/Style/CSS/',
'PHP' => 'PHP: https://www.php.net',
];
$parseMulti = static function (string $body) use ($catalog): array {
$post = [];
parse_str($body, $post);
$q = $post['q'] ?? '';
if (!is_array($q)) {
return ['is_array' => false, 'matched' => []];
}
$lines = [];
foreach ($q as $val) {
if (is_string($val) && isset($catalog[$val])) {
$lines[] = $catalog[$val];
}
}
return ['is_array' => true, 'matched' => $lines];
};
$actual = [
'multi_select' => $parseMulti('q[]=HTML&q[]=PHP'),
'checkbox_with_unknown' => $parseMulti('q[]=CSS&q[]=UNKNOWN&q[]=PHP'),
'none_selected' => $parseMulti(''),
];
$expected = [
'multi_select' => [
'is_array' => true,
'matched' => [
'HTML5: https://html.spec.whatwg.org',
'PHP: https://www.php.net',
],
],
'checkbox_with_unknown' => [
'is_array' => true,
'matched' => [
'CSS: https://www.w3.org/Style/CSS/',
'PHP: https://www.php.net',
],
],
'none_selected' => ['is_array' => false, 'matched' => []],
];
$cases[] = [
'name' => 'select_multi_and_checkbox_array',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
// Case 4: Server-side validation fallback when client-side constraints are bypassed
(function () use (&$cases): void {
$validateServer = static function (array $input): array {
$errors = [];
$name = trim((string)($input['fname'] ?? ''));
$ageRaw = trim((string)($input['age'] ?? ''));
if ($name === '') {
$errors[] = 'fname_required';
}
$ageInt = filter_var($ageRaw, FILTER_VALIDATE_INT, ['options' => ['min_range' => 1, 'max_range' => 120]]);
if ($ageInt === false) {
$errors[] = 'age_out_of_range';
}
return ['valid' => count($errors) === 0, 'errors' => $errors];
};
$actual = [
'bypassed_bad_payload' => $validateServer(['fname' => ' ', 'age' => '-5']),
'valid_payload' => $validateServer(['fname' => '林青', 'age' => '24']),
];
$expected = [
'bypassed_bad_payload' => ['valid' => false, 'errors' => ['fname_required', 'age_out_of_range']],
'valid_payload' => ['valid' => true, 'errors' => []],
];
$cases[] = [
'name' => 'server_vs_client_validation_flow',
'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-forms-lab/explore-forms.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-forms.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1251.zip