PHP ?? ??? ??
PHP ??? ?? ?? ??? ???? ???.
? ???? ??? ??? ???? ?? PHP ?? ???? ?? ??? ?????.
? ?? ??? HTML ???? ?? ?? ??? ???? ????. ??? ??? ??, ??? ??? ????? ? , ?? ??:
Instance
<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>PHP.cn</title> </head> <body> <h2>PHP 表單驗(yàn)證實(shí)例</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 名字: <input type="text" name="name" value=""> <br> E-mail: <input type="text" name="email" value=""> <br> 網(wǎng)址: <input type="text" name="website" value=""> <br> 備注: <textarea name="comment" rows="5" cols="40"></textarea> <br> 性別: <input type="radio" name="gender" value="female">女 <input type="radio" name="gender" value="male">男 <br> <input type="submit" name="submit" value="提交"> </form> </body> </html>
???? ?? ??:
? ?? ??? ?? ??? ??? ????.
| ?> ??? ?? ???>?> | ||||||||||||
?? | ?????. +??? ??? ??? ? ???? | ||||||||||||
??? | URL | ????. ?? ?? ??? URL? ???? ???. | |||||||||||
?? | ??? ??
'??', '???', '????' ??? ??? ?? ????, '??' ??? ??? ?? ??? ?????. HTML ??? ??? ????.
??? ?? "??" ??? ??? ?????. HTML ??? ??? ????.
?? ?? HTML ?? ??? ??? ????.
??? ???? ???? ?? method="post" ???? ?????. $_SERVER["PHP_SELF"] ??? ?????? $_SERVER["PHP_SELF"]? ?? ?? ?? ????? ?? ??? ???? ???? ?????. ??? $_SERVER["PHP_SELF"]? ?? ???? ???? ?? ?? ???? ??? ??? ????. ??? ???? ???? ?? ????? ?? ??? ??? ?? ? ????. htmlspecialchars() ??? ?????? htmlspecialchars() ??? ?? ??? HTML ???? ?????. ?? < ? >? ?? HTML ??? < ??? ?? ???? HTML ?? JavaScript ??? ??? ???? ??? ???? ?? ??? ? ????(?? ??? ???? ??). PHP ?? ??? ?? ?? ? $_SERVER["PHP_SELF"] ??? ??? ?? ??? ? ????! ????? PHP_SELF? ???? ?? ???? ??? ???? CSS??? ?? XSS(?? ??? ????)? ??? ? ????. ? ?? XSS? ???? ???? ?? ???? ?? ? ???? ????? ? ????? ??? ? ????. ?? ??? "test_form.php"?? ???? ??? ?????.
?? URL? ???? ?? ?? "test_form.php"? ?????. ? ??? ??? ?? ?????. <form method=" post " action="test_form.php">??? ???? ???? ?? ???? ?? ??? ????? ?? ?????.http://m.miracleart.cn/test_form.php/% 22 %3E%3Cscript%3Ealert('hacked')%3C/script%3E <form method="post" action="test_form.php/"><script>alert('hacked')</script> ??? ???? ??? ?????, ?? ??? ???????. ? Javascript ??? ???? ??? ? ?????(????? ?? ??? ???). ?? ??? PHP_SELF ??? ??? ??? ? ??? ???? ??? ????. ?? ?? JavaScript ??? <script> ??? ??? ? ????! ??? ?? ??? ???? ?? ??? ????? ? ????. ?? ??? ?? ??? ????? ???? ?? ???? ?? ? ????. $_SERVER["PHP_SELF"] ??? ???? ??? ?????? $ _SERVER ["PHP_SELF"]? htmlspecialchars() ??? ???? ?? ? ????. ? ??? ??? ????:
htmlspecialchars() ?? ??? ?? ??? HTML ???? ?????. ?? ???? PHP_SELF ??? ????? ?? ??? ??? ?? ?????.
? ???? ???? ?????! PHP? ?? ?? ??? ??? ??
?? ?? ?? ? ?? PHP? htmlspecialchars() ??? ?? ?? ??? ???? ????. htmlspecialchars() ??? ??? ? ???? ??? ??? ??? ????? ??:
- ??? ??? ?? ????? ??? ????? ???? ????.
?? ? ??? ???? ???? ????? ???? ?????. ???? ??? ???? ?? ? ?? ??? ? ???? ???. 1. (PHP Trim() ??? ??) ??? ?? ????? ???? ??(?? ??)? ?????. , ?, ? ??) 2. (PHP ??????() ??? ??) ??? ?? ????? ???? ??() ???? ?? ??? ????(??? ???? ???? ?? ??). ?? ????? ????). ?? ??? test_input()?? ??????. ?? test_input() ??? ?? ? $_POST ??? ??? ? ????. ????? ??? ????. ? <!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>PHP中文網(wǎng)(php.cn)</title> </head> <body> <?php // 定義變量并默認(rèn)設(shè)置為空值 $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; } ?> <h2>PHP 表單驗(yàn)證實(shí)例</h2> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 名字: <input type="text" name="name"> <br><br> E-mail: <input type="text" name="email"> <br><br> 網(wǎng)址: <input type="text" name="website"> <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">男 <br><br> <input type="submit" name="submit" value="提交"> </form> <?php echo "<h2>您輸入的內(nèi)容是:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> ????? ???? ????? ??: ? ????? ??? ? $_SERVER["REQUEST_METHOD"]? ???? ??? ?????? ?????. REQUEST_METHOD? POST?? ??? ???? ???? ???? ?????. ??? ???? ??? ??? ??? ???? ???? ?????. ? ???? ????? ??? ??????, ???? ??? ???? ???? ??? ????? ??? ? ????. ?? ???? ???? ??? ???? ???? ???? ??? ?????. ||
<!DOCTYPE HTML>
<html>
<head>
<meta charset="utf-8">
<title>PHP.cn</title>
</head>
<body>
<h2>PHP 表單驗(yàn)證實(shí)例</h2>
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
名字: <input type="text" name="name" value="">
<br>
E-mail: <input type="text" name="email" value="">
<br>
網(wǎng)址: <input type="text" name="website" value="">
<br>
備注: <textarea name="comment" rows="5" cols="40"></textarea>
<br>
性別:
<input type="radio" name="gender" value="female">女
<input type="radio" name="gender" value="male">男
<br>
<input type="submit" name="submit" value="提交">
</form>
</body>
</html>
?? ????? ????? ? ????. ?? ???? ???? ????. ???? ? ??? ?? ?? ??????~
? ??? ??? ???? ???? ????.
|