-
$_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 字符会被替换为 < 和 > 。这样可防止攻击者通过在表单中注入 HTML 或 JavaScript 代码(跨站点脚本攻击)对代码进行利用。
如何攻击该方式? -
检查函数
// 定义变量并设置为空值
$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;
}
【PHP学习笔记】
Author: 紫炁
License: Copyright (c) 2019 CC-BY-NC-4.0 LICENSE
Slogan: Do you believe in DESTINY?