• $_SERVER[“PHP_SELF”]是一种超全局变量,返回当前执行脚本的文件名。

    • “test_form.php” 的页面中有如下表单:

      <form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">

      用户输入http://www.example.com/test_form.php转化为:

      <form method="post" action="test_form.php">

      http://www.example.com/test_form.php/**%22%3E%3C**script **%3**Ealer('hacked')**%3C**/script **%3E**则转为:

      <form method="post" action="test_form.php"/><script>alert('hacked')</script>

      执行javascript代码。

  • htmlspecialchars()把特殊字符转换为HTML实体,< 和 > 之类的 HTML 字符会被替换为 &lt; 和 &gt; 。这样可防止攻击者通过在表单中注入 HTML 或 JavaScript 代码(跨站点脚本攻击)对代码进行利用。
    如何攻击该方式?

  • 检查函数

    <?php
    // 定义变量并设置为空值
    $name = $email = $gender = $comment = $website = "";

    if ($_SERVER["REQUEST_METHOD"] == "POST") {
    $name = test_input($_POST["name"]);
    $email = test_input($_POST["email"]);
    $website = test_input($_POST["website"]);
    $comment = test_input($_POST["comment"]);
    $gender = test_input($_POST["gender"]);
    }

    function test_input($data) {
    $data = trim($data);
    $data = stripslashes($data);
    $data = htmlspecialchars($data);
    return $data;
    }
    ?>