PHP语言之表单进阶
第一关
/********** Begin *********/ function formatted_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } /********** End *********/
第二关
/********** Begin *********/ if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (empty($_POST['name'])) { $nameErr = '名字是必需的'; } else { $name = formatted_input($_POST['name']); } if (empty($_POST['email'])) { $emailErr = '邮箱是必需的'; } else { $email = formatted_input($_POST['email']); } if (empty($_POST['website'])) { $website = ''; } else { $website = formatted_input($_POST['website']); } if (empty($_POST['comment'])) { $comment = ''; } else { $comment = formatted_input($_POST['comment']); } if (empty($_POST['gender'])) { $genderErr = '性别是必需的'; } else { $gender = formatted_input($_POST['gender']); } } /********** End *********/
第三关
/********** Begin *********/ if (!preg_match("/^[a-zA-Z ]*$/", $name)) { $nameErr = '只允许字母和空格'; $name = ''; //格式非法,将$name变量清空 } /********** End *********/ /********** Begin *********/ if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = '非法的邮箱格式'; $email = ''; //格式非法,将$email变量清空 } /********** End *********/ /********** Begin *********/ if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = '非法的 URL 地址'; $website = ''; //格式非法,将$website变量清空 } /********** End *********/
第四关
/********** Begin *********/ $nameErr = $emailErr = $genderErr = $websiteErr = ''; $name = $email = $gender = $comment = $website = ''; if ($_SERVER['REQUEST_METHOD'] == 'POST') { if (empty($_POST['name'])) { $nameErr = '名字是必需的'; } else { $name = formatted_input($_POST['name']); if (!preg_match("/^[a-zA-Z ]*$/", $name)) { $nameErr = '只允许字母和空格'; $name = ''; //格式非法,将$name变量清空 } } if (empty($_POST['email'])) { $emailErr = '邮箱是必需的'; } else { $email = formatted_input($_POST['email']); if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) { $emailErr = '非法的邮箱格式'; $email = ''; //格式非法,将$email变量清空 } } if (empty($_POST['website'])) { $website = ''; } else { $website = formatted_input($_POST['website']); if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) { $websiteErr = '非法的 URL 地址'; $website = ''; //格式非法,将$website变量清空 } } if (empty($_POST['comment'])) { $comment = ''; } else { $comment = formatted_input($_POST['comment']); } if (empty($_POST['gender'])) { $genderErr = '性别是必需的'; } else { $gender = formatted_input($_POST['gender']); } } function formatted_input($data) { $data = trim($data); $data = stripslashes($data); $data = htmlspecialchars($data); return $data; } /********** End *********/ <!-- Begin --> <form method="post" action="<?php echo htmlspecialchars($_SERVER['PHP_SELF']);?>"> <label> 姓名:<input type="text" name="name"> </label> <span class="error">* <?php echo $nameErr;?></span> <br><br> <label> 邮箱:<input type="text" name="email"> <span class="error">* <?php echo $emailErr;?></span> </label> <br><br> <label> 博客:<input type="text" name="website"> <span class="error"><?php echo $websiteErr;?></span> </label> <br><br> <label> 备注:<textarea name="comment" rows="5" cols="40"></textarea> </label> <br><br> <label> 性别:<input type="radio" name="gender" value="male">男 <input type="radio" name="gender" value="female">女 <span class="error">* <?php echo $genderErr;?></span> </label> <br><br> <input type="submit" name="submit" value="提交"> </form> <!-- End -->