$_GET 和 $_POST
提示:开发者偏爱 POST 来发送表单数据。
- PHP 超全局变量 $_GET 和 $_POST 用于收集表单数据。
何时使用 GET?
$_GET 是通过 URL 参数传递到当前脚本的变量数组。
通过 GET 方法从表单发送的信息会显示在 URL 中,绝不能使用 GET 来发送密码或其他敏感信息!。
由于变量显示在 URL 中,把页面添加到书签中也更为方便。
GET 对所发送信息的数量也有限制,限制在 2000 个字符。
何时使用 POST?
$_POST 是通过 HTTP POST 传递到当前脚本的变量数组。
发送的信息不显示在 URL 中,而是嵌入 HTTP 请求的主体中,相对安全。
由于变量未显示在 URL 中,也就无法将页面添加到书签。
对所发送信息的数量无限制。
POST 支持高阶功能,比如在向服务器上传文件时进行 multi-part 二进制输入。
PHP 表单验证 link
htmlspecialchars()
通过使用 htmlspecialchars() 函数能够避免 $_SERVER[“PHP_SELF”] 被利用。
htmlspecialchars() 函数把特殊字符转换为 HTML 实体。这意味着 < 和 > 之类的 HTML 字符会被替换为 < 和 > 。这样可防止攻击者通过在表单中注入 HTML 或 JavaScript 代码(跨站点脚本攻击)对代码进行利用。
<script>location.href('http://www.hacked.com')</script>
// 转义代码:
<script>location.href('http://www.hacked.com')</script>
- 未使用 htmlspecialchars()
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
如果用户在地址栏中追加如下 URL:
http://localhost/PHPDemo/index.php
http://localhost/PHPDemo/index.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E
在这种情况下,上面的代码会转换为:
<form method="post" action="http://localhost/PHPDemo/index.php"/><script>alert('hacked')</script>
这时,页面已经被修改了(用户会看到一个提示框)(Chrome浏览器会拦截这种情况)。您应该意识到
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>Name:<input type="text" name="name" value=""></label>
<input type="submit" name="" value="提交">
</form>
未使用:
<form method="post" action="http://localhost/PHPDemo/index.php"/><script>alert('hacked')</script>
使用:
<form method="post" action="http://localhost/PHPDemo/index.php/"><script>alert('hacked')</script>">
PHP 表单验证函数
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title>Document</title>
<style type="text/css">
.error {
color: red;
}
</style>
</head>
<body>
<?php
$username = $password = $email = $gender = "";
$usernameErr = $passwordErr = $emailErr = $genderErr = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["username"])) {
$usernameErr = "username is required";
} else {
$username = test_input($_POST["username"]);
if (!preg_match("/^[a-zA-Z0-9]*$/",$username)) {
$usernameErr = "只允许字母和数字";
}
}
if (empty($_POST["password"])) {
$passwordErr = "password is required";
} else {
$password = test_input($_POST["password"]);
if (!preg_match("/^[a-zA-Z0-9]*$/",$password)) {
$passwordErr = "只允许字母和数字";
}
}
if (empty($_POST["email"])) {
$emailErr = "";
} else {
$email = test_input($_POST["email"]);
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "无效的 email 格式";
}
}
if (empty($_POST["gender"])) {
$genderErr = "性别是必填项";
} else {
$gender = test_input($_POST["gender"]);
}
}
function test_input($data) {
// PHP 验证表单数据的检查函数
// 1.通过 PHP trim() 函数 去除用户输入数据中不必要的字符(多余的空格、制表符、换行)
// 2.通过 PHP stripslashes() 函数 删除用户输入数据中的反斜杠(\)
// 3.通过 htmlspecialchars() 函数 把特殊字符转换为 HTML 实体
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
?>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
<label>用户:<input type="text" name="username" value="<?php echo $username; ?>" required="required"></label>
<span class="error">* <?php echo $usernameErr;?></span><br>
<label>密码:<input type="password" name="password" value="<?php echo $password; ?>" required="required"></label>
<span class="error">* <?php echo $passwordErr;?></span><br>
<label>邮箱:<input type="email" name="email" value="<?php echo $email; ?>"></label>
<span class="error"><?php echo $emailErr;?></span><br>
性别:
<label><input type="radio" name="gender" <?php if (isset($gender) && $gender == "female") echo "checked";?> value="female">Female</label>
<label><input type="radio" name="gender" <?php if (isset($gender) && $gender == "male") echo "checked";?> value="male">Male</label>
<span class="error">* <?php echo $genderErr;?></span><br>
<input type="submit" name="submit" value="提交">
</form>
<?php
echo "<h2>您的输入:</h2>";
echo $username . "<br>";
echo $password . "<br>";
echo $email . "<br>";
echo $gender . "<br>";
?>
</body>
</html>