<!DOCTYPE HTML> <html> <head> <meta charset="utf-8"> <title>菜鳥教程(runoob.com)</title> <style> .error {color: #FF0000;} </style> </head> <body> <?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 (!preg_match("/^[a-zA-Z ]*$/",$name)) { $nameErr = "只容許字母和空格"; } } if (empty($_POST["email"])) { $emailErr = "郵箱是必需的"; } 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"]); } } function test_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } ?> <h2>PHP 表單驗證明例</h2> <p><span class="error">* 必需字段。</span></p> <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 名字: <input type="text" name="name" value="<?php echo $name;?>"> <span class="error">* <?php echo $nameErr;?></span> <br><br> E-mail: <input type="text" name="email" value="<?php echo $email;?>"> <span class="error">* <?php echo $emailErr;?></span> <br><br> 網址: <input type="text" name="website" value="<?php echo $website;?>"> <span class="error"><?php echo $websiteErr;?></span> <br><br> 備註: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> <br><br> 性別: <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">女 <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">男 <span class="error">* <?php echo $genderErr;?></span> <br><br> <input type="submit" name="submit" value="Submit"> </form> <?php echo "<h2>您輸入的內容是:</h2>"; echo $name; echo "<br>"; echo $email; echo "<br>"; echo $website; echo "<br>"; echo $comment; echo "<br>"; echo $gender; ?> </body> </html>
首先讓咱們先看看純HTML的表單代碼分析:php
"名字", "E-mail", 及"網址"字段爲文本輸入元素,"備註"字段是 textarea。html
"性別"字段是單選按鈕web
HTML 表單代碼以下所示:瀏覽器
<form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>">
$_SERVER["PHP_SELF"]是超級全局變量,返回當前正在執行腳本的文件名,與 document root相關。 因此, $_SERVER["PHP_SELF"] 會發送表單數據到當前頁面,而不是跳轉到不一樣的頁面。服務器
htmlspecialchars() 函數把一些預約義的字符轉換爲 HTML 實體。函數
預約義的字符是: & (和號) 成爲 & " (雙引號) 成爲 " ' (單引號) 成爲 ' < (小於) 成爲 < > (大於) 成爲 >
考慮到用戶會在瀏覽器地址欄中輸入如下地址:http://www.runoob.com/test_form.php/"><script>alert('hacked')</script> 以上的 URL 中,將被解析爲以下代碼並執行:post
<form method="post" action="test_form.php/"><script>alert('hacked')</script>
請注意, 任何JavaScript代碼能夠添加在<script>標籤中! 黑客能夠利用這點重定向頁面到另一臺服務器的頁面上,頁面 代碼文件中能夠保護惡意代碼,代碼能夠修改全局變量或者獲取用戶的表單數據。spa
<form method="post" action="test_form.php/"><script>alert('hacked')</script>">
嘗試該漏洞失敗!code
當用戶提交表單時,咱們將作如下兩件事情,:orm