<?php
declare(strict_types=1);
function test_input(string $data): string
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data, ENT_QUOTES, 'UTF-8');
return $data;
}
$cases = [];
// Case 1: PHP_SELF XSS attack vs htmlspecialchars mitigation
(function () use (&$cases): void {
$craftedPhpSelf = '/test_form.php/"><script>alert(\'hacked\')</script>';
$unsafeFormTag = '<form method="post" action="' . $craftedPhpSelf . '">';
$safeFormTag = '<form method="post" action="' . htmlspecialchars($craftedPhpSelf, ENT_QUOTES, 'UTF-8') . '">';
$actual = [
'unsafe_contains_script_tag' => str_contains($unsafeFormTag, '<script>'),
'safe_contains_script_tag' => str_contains($safeFormTag, '<script>'),
'safe_form_tag' => $safeFormTag,
];
$expected = [
'unsafe_contains_script_tag' => true,
'safe_contains_script_tag' => false,
'safe_form_tag' => '<form method="post" action="/test_form.php/"><script>alert('hacked')</script>">',
];
$cases[] = [
'name' => 'php_self_xss_escape',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
// Case 2: Five predefined HTML entities in htmlspecialchars(ENT_QUOTES)
(function () use (&$cases): void {
$chars = ['&', '"', "'", '<', '>'];
$mapped = [];
foreach ($chars as $ch) {
$mapped[$ch] = htmlspecialchars($ch, ENT_QUOTES, 'UTF-8');
}
$scriptInput = "<script>location.href='https://example.org'</script>";
$actual = [
'entities' => $mapped,
'escaped_script' => htmlspecialchars($scriptInput, ENT_QUOTES, 'UTF-8'),
];
$expected = [
'entities' => [
'&' => '&',
'"' => '"',
"'" => ''',
'<' => '<',
'>' => '>',
],
'escaped_script' => '<script>location.href='https://example.org'</script>',
];
$cases[] = [
'name' => 'htmlspecialchars_five_entities',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
// Case 3: test_input pipeline (trim -> stripslashes -> htmlspecialchars)
(function () use (&$cases): void {
$raw = " \tO\\'Reilly <b>&</b> \"PHP\" \r\n";
$actual = [
'after_trim' => trim($raw),
'after_stripslashes' => stripslashes(trim($raw)),
'after_test_input' => test_input($raw),
];
$expected = [
'after_trim' => "O\\'Reilly <b>&</b> \"PHP\"",
'after_stripslashes' => "O'Reilly <b>&</b> \"PHP\"",
'after_test_input' => 'O'Reilly <b>&</b> "PHP"',
];
$cases[] = [
'name' => 'test_input_trim_stripslashes_escape',
'passed' => $actual === $expected,
'expected' => $expected,
'actual' => $actual,
];
})();
// Case 4: REQUEST_METHOD dispatch (GET skips validation, POST cleans all 5 fields)
(function () use (&$cases): void {
$handleRequest = static function (string $method, array $post): array {
$name = $email = $gender = $comment = $website = '';
if ($method === 'POST') {
$name = test_input((string)($post['name'] ?? ''));
$email = test_input((string)($post['email'] ?? ''));
$website = test_input((string)($post['website'] ?? ''));
$comment = test_input((string)($post['comment'] ?? ''));
$gender = test_input((string)($post['gender'] ?? ''));
}
return compact('name', 'email', 'website', 'comment', 'gender');
};
$actual = [
'get_initial' => $handleRequest('GET', ['name' => 'Ignored']),
'post_cleaned' => $handleRequest('POST', [
'name' => ' Alice Lin ',
'email' => " alice@example.org\n",
'website' => 'https://example.org',
'comment' => "Hello <script>x</script>",
'gender' => 'female',
]),
];
$expected = [
'get_initial' => ['name' => '', 'email' => '', 'website' => '', 'comment' => '', 'gender' => ''],
'post_cleaned' => [
'name' => 'Alice Lin',
'email' => 'alice@example.org',
'website' => 'https://example.org',
'comment' => 'Hello <script>x</script>',
'gender' => 'female',
],
];
$cases[] = [
'name' => 'request_method_post_dispatch',
'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-form-validation-lab/explore-form-validation.php
本地运行步骤快速上手
- 点击上方“保存源码”保存为
explore-form-validation.php,或下载右侧课程工程包。 - 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
- 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载
课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip
课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1250.zip