文章目录
一、PHP
1.php表单
1.php表单和用户输入
php中的 g e t 和 _get和 get和_post变量用于检索表单中的信息
php 能把来自 HTML 页面中的表单元素自动变成可供 PHP 脚本使用。
form.html 文件代码:
<html>
<head>
<meta charset="utf-8">
<title>fighting</title>
</head>
<body>
<form action="welcome.php" method="post">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>
</body>
</html>
welcome.php 文件代码
欢迎<?php echo $_POST["fname"]; ?>!<br>
你的年龄是 <?php echo $_POST["age"]; ?> 岁。
表单验证尽可能的对用户输入进行验证。
$_GET 变量接受所有以 get 方式发送的请求,及浏览器地址栏中的 ? 之后的内容。
$_POST 变量接受所有以 post 方式发送的请求,例如,一个 form 以 method=post 提交,提交后 php 会处理 post 过来的全部变量。
$_REQUEST 支持两种方式发送过来的请求,即 post 和 get 它都可以接受,显示不显示要看传递方法,get 会显示在 url 中(有字符数限制),post 不会在 url 中显示,可以传递任意多的数据(只要服务器支持)。
2.PHP表单验证
1.表单元素
html表单代码
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
$_SERVER[“PHP_SELF”]是超级全局变量,返回当前正在执行脚本的文件名,与 document root相关所以, $_SERVER[“PHP_SELF”] 会发送表单数据到当前页面,而不是跳转到不同的页面。
htmlspecialchars() 函数把一些预定义的字符转换为 HTML 实体。
& (和号) 成为 &
" (双引号) 成为 "
' (单引号) 成为 '
< (小于) 成为 <
> (大于) 成为 >
$_server[“php_self”]变量可能会被黑客利用
可使用跨网站脚本http链接进行攻击, $_server[“php_self”]服务器变量也会被植入脚本。原因是跨网站脚本是附在执行文件的路径后面的,因此 $_server[“php_self”]字符串就会包含http链接后面的js代码。
XSS又叫 CSS (Cross-Site Script) ,跨站脚本攻击。恶意攻击者往Web页面里插入恶意html代码,当用户浏览该页之时,嵌入其中Web里面的html代码会被执行,从而达到恶意用户的特殊目的。
#指定以下表单文件名为 "test_form.php":
<form method="post" action="<?php echo $_SERVER["PHP_SELF"];?>">
#现在,我们使用URL来指定提交地址 "test_form.php",以上代码修改为如下所示:
<form method="post" action="test_form.php">
考虑到用户会在浏览器地址栏中输入以下地址:
http://www.runoob.com/test_form.php/%22%3E%3Cscript%3Ealert('hacked')%3C/script%3E
URL 中,将被解析为如下代码并执行:
<form method="post" action="test_form.php/"><script>alert('hacked')</script>
代码中添加script标签,和alter命令,当页面载入时会执行js代码。因此黑客会应用
1.如何避免 $_server[“php_self”]被利用?
可通过htmlspecialchars()函数来避免被利用
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
#htmlspecialchars() 把一些预定义的字符转换为 HTML 实体。现在如果用户想利用 PHP_SELF 变量, 结果将输出如下所示:
<form method="post" action="test_form.php/"><script>alert('hacked')</script>">
2.使用php验证表单数据
使用 PHP trim() 函数去除用户输入数据中不必要的字符 (如:空格,tab,换行)。
使用PHP stripslashes()函数去除用户输入数据中的反斜杠 ()
<?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;
}
?>
通过$_SERVER[“REQUEST_METHOD”]来检测表单是否被提交 。
3.php表单必需字段
1.必需字段
检查$_post变量是否为空,使用php的empty()函数。如果为空则显示错误信息。
<?php
// 定义变量并默认设为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "名字是必需的。";
} else {
$name = test_input($_POST["name"]);
}
if (empty($_POST["email"])) {
$emailErr = "邮箱是必需的。";
} else {
$email = test_input($_POST["email"]);
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "性别是必需的。";
} else {
$gender = test_input($_POST["gender"]);
}
}
?>
2.显示错误信息
为每个字段添加一些脚本,各个脚本会在信息输入时显示错误信息。用户
没填写信息就提交表单则会输出错误信息
<form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>">
名字: <input type="text" name="name">
<span class="error">* <?php echo $nameErr;?></span>
<br><br>
E-mail: <input type="text" name="email">
<span class="error">* <?php echo $emailErr;?></span>
<br><br>
网址: <input type="text" name="website">
<span class="error"><?php echo $websiteErr;?></span>
<br><br>
备注: <textarea name="comment" rows="5" cols="40"></textarea>
<br><br>
性别:
<input type="radio" name="gender" value="female">女
<input type="radio" name="gender" value="male">男
<span class="error">* <?php echo $genderErr;?></span>
<br><br>
<input type="submit" name="submit" value="Submit">
</form>
4.验证邮件和url
1.验证名称
检测name字段是否只包含字母和空格
$name = test_input($_POST["name"]);
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "只允许字母和空格";
}
#preg_match — 进行正则表达式匹配。
语法:
int preg_match ( string $pattern , string $subject [, array $matches [, int $flags ]] )
在 subject 字符串中搜索与 pattern 给出的正则表达式相匹配的内容。如果提供了 matches ,则其会被搜索的结果所填充。$matches[0] 将包含与整个模式匹配的文本,$matches[1] 将包含与第一个捕获的括号中的子模式所匹配的文本,以此类推。
2.验证邮件和url
<?php
// 定义变量并默认设置为空值
$nameErr = $emailErr = $genderErr = $websiteErr = "";
$name = $email = $gender = $comment = $website = "";
if ($_SERVER["REQUEST_METHOD"] == "POST") {
if (empty($_POST["name"])) {
$nameErr = "Name is required";
} else {
$name = test_input($_POST["name"]);
// 检测名字是否只包含字母跟空格
if (!preg_match("/^[a-zA-Z ]*$/",$name)) {
$nameErr = "只允许字母和空格";
}
}
if (empty($_POST["email"])) {
$emailErr = "Email is required";
} else {
$email = test_input($_POST["email"]);
// 检测邮箱是否合法
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = "非法邮箱格式";
}
}
if (empty($_POST["website"])) {
$website = "";
} else {
$website = test_input($_POST["website"]);
// 检测 URL 地址是否合法
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = "非法的 URL 的地址";
}
}
if (empty($_POST["comment"])) {
$comment = "";
} else {
$comment = test_input($_POST["comment"]);
}
if (empty($_POST["gender"])) {
$genderErr = "性别是必需的";
} else {
$gender = test_input($_POST["gender"]);
}
}
?>
5.$_get变量
$_get变量用来收集来自method="get"表单中的值
从get发送的表单信息,对任何人都是可见的,对发送的信息也是有限制
#form.html
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<form action="welcome.php" method="get">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>
</body>
</html>
#weclom.php
欢迎 <?php echo $_GET["fname"]; ?>!<br>
你的年龄是 <?php echo $_GET["age"]; ?> 岁。
http://www.runoob.com/welcome.php?fname=Runoob&age=3
使用敏感信息时,不应该使用这个方法。
因为变量显示在url中,因此可以在收藏夹中显示该页面
6.$_post变量
1.$_post变量
post方法表单发送的信息,对任何人是不可见的,发送的信息量也是没有限制
form.html
<html>
<head>
<meta charset="utf-8">
<title>菜鸟教程(runoob.com)</title>
</head>
<body>
<form action="welcome.php" method="post">
名字: <input type="text" name="fname">
年龄: <input type="text" name="age">
<input type="submit" value="提交">
</form>
</body>
</html>
welcome.php
欢迎 <?php echo $_POST["fname"]; ?>!<br>
你的年龄是 <?php echo $_POST["age"]; ?> 岁。
http://www.runoob.com/welcome.php
2.$_requst变量
预定义的 $_REQUEST 变量包含了 G E T 、 _GET、 GET、_POST 和 $_COOKIE 的内容。
$_REQUEST 变量可用来收集通过 GET 和 POST 方法发送的表单数据。
总结
一个人过最高的精神自由,必须过最低
物质要求。