蓝色天空.知识 · 实践 · 创造
我的收藏
在线代码实验室

运行 PHP_SELF XSS 转义、五字符实体编码、test_input 清洗与 POST 分发实验

PHPWindows x64、PHP 8.4 本地 CLI 环境。explore-form-validation.php
explore-form-validation.php130 行
<?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/&quot;&gt;&lt;script&gt;alert(&#039;hacked&#039;)&lt;/script&gt;">',
    ];
    $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' => [
            '&' => '&amp;',
            '"' => '&quot;',
            "'" => '&#039;',
            '<' => '&lt;',
            '>' => '&gt;',
        ],
        'escaped_script' => '&lt;script&gt;location.href=&#039;https://example.org&#039;&lt;/script&gt;',
    ];
    $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&#039;Reilly &lt;b&gt;&amp;&lt;/b&gt; &quot;PHP&quot;',
    ];
    $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 &lt;script&gt;x&lt;/script&gt;',
            '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

本地运行步骤快速上手
  1. 点击上方“保存源码”保存为 explore-form-validation.php,或下载右侧课程工程包。
  2. 确认本地已准备 Windows x64、PHP 8.4 本地 CLI 环境。。
  3. 按对应章节命令执行,结合正文核对实际输出。
附件与关联工程包下载

下载课程关联工程包:PHP

课程关联工程包(登记关联95章) · 含当前章节或实例 · mbskycom202612a1232.zip

下载课程关联工程包:PHP

课程关联工程包(登记关联1章) · 含当前章节或实例 · mbskycom202612a1250.zip
SITE SEARCH

搜索教程、工具与资讯

热门搜索

JavaScriptJSON格式化正则表达式AI 编程AI 翻译AI导航专题标签
Esc 关闭

欢迎登录蓝色天空网

使用会员账号,继续你的学习