1.htmlspecialchars()php
htmlspecialchars() 函數把一些預約義的字符轉換爲 HTML 實體。預約義的字符是:html
2.通常用到的驗證輸入的函數:web
function test_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}函數
3.preg_match("/^[a-zA-Z ]*$/",$name) 匹配只包含字母和空格的狀況,多用於驗證名字。post
preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email) 對郵箱進行驗證spa
preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website) 對郵箱進行驗證code
4.一個完整的表單驗證程序orm
1 <!DOCTYPE HTML> 2 <html> 3 <head> 4 <meta charset="utf-8"> 5 <title>XXXXXXX</title> 6 <style> 7 .error {color: #FF0000;} 8 </style> 9 </head> 10 <body> 11 12 <?php 13 // 定義變量並默認設置爲空值 14 $nameErr = $emailErr = $genderErr = $websiteErr = ""; 15 $name = $email = $gender = $comment = $website = ""; 16 17 if ($_SERVER["REQUEST_METHOD"] == "POST") 18 { 19 if (empty($_POST["name"])) 20 { 21 $nameErr = "名字是必需的"; 22 } 23 else 24 { 25 $name = test_input($_POST["name"]); 26 // 檢測名字是否只包含字母跟空格 27 if (!preg_match("/^[a-zA-Z ]*$/",$name)) 28 { 29 $nameErr = "只容許字母和空格"; 30 } 31 } 32 33 if (empty($_POST["email"])) 34 { 35 $emailErr = "郵箱是必需的"; 36 } 37 else 38 { 39 $email = test_input($_POST["email"]); 40 // 檢測郵箱是否合法 41 if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) 42 { 43 $emailErr = "非法郵箱格式"; 44 } 45 } 46 47 if (empty($_POST["website"])) 48 { 49 $website = ""; 50 } 51 else 52 { 53 $website = test_input($_POST["website"]); 54 // 檢測 URL 地址是否合法 55 if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) 56 { 57 $websiteErr = "非法的 URL 的地址"; 58 } 59 } 60 61 if (empty($_POST["comment"])) 62 { 63 $comment = ""; 64 } 65 else 66 { 67 $comment = test_input($_POST["comment"]); 68 } 69 70 if (empty($_POST["gender"])) 71 { 72 $genderErr = "性別是必需的"; 73 } 74 else 75 { 76 $gender = test_input($_POST["gender"]); 77 } 78 } 79 80 function test_input($data) 81 { 82 $data = trim($data); 83 $data = stripslashes($data); 84 $data = htmlspecialchars($data); 85 return $data; 86 } 87 ?> 88 89 <h2>PHP 表單驗證明例</h2> 90 <p><span class="error">* 必需字段。</span></p> 91 <form method="post" action="<?php echo htmlspecialchars($_SERVER["PHP_SELF"]);?>"> 92 名字: <input type="text" name="name" value="<?php echo $name;?>"> 93 <span class="error">* <?php echo $nameErr;?></span> 94 <br><br> 95 E-mail: <input type="text" name="email" value="<?php echo $email;?>"> 96 <span class="error">* <?php echo $emailErr;?></span> 97 <br><br> 98 網址: <input type="text" name="website" value="<?php echo $website;?>"> 99 <span class="error"><?php echo $websiteErr;?></span> 100 <br><br> 101 備註: <textarea name="comment" rows="5" cols="40"><?php echo $comment;?></textarea> 102 <br><br> 103 性別: 104 <input type="radio" name="gender" <?php if (isset($gender) && $gender=="female") echo "checked";?> value="female">女 105 <input type="radio" name="gender" <?php if (isset($gender) && $gender=="male") echo "checked";?> value="male">男 106 <span class="error">* <?php echo $genderErr;?></span> 107 <br><br> 108 <input type="submit" name="submit" value="Submit"> 109 </form> 110 111 <?php 112 echo "<h2>您輸入的內容是:</h2>"; 113 echo $name; 114 echo "<br>"; 115 echo $email; 116 echo "<br>"; 117 echo $website; 118 echo "<br>"; 119 echo $comment; 120 echo "<br>"; 121 echo $gender; 122 ?> 123 124 </body> 125 </html>