头歌php 表单语言进阶
PHP语言之表单进阶
第一关
function formatted_input($data)
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
return $data;
}
第二关
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']);
}
}
第三关
if (!preg_match("/^[a-zA-Z ]*$/", $name)) {
$nameErr = '只允许字母和空格';
$name = ''; //格式非法,将$name变量清空
}
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/",$email)) {
$emailErr = '非法的邮箱格式';
$email = ''; //格式非法,将$email变量清空
}
if (!preg_match("/\b(?:(?:https?|ftp):\/\/|www\.)[-a-z0-9+&@#\/%?=~_|!:,.;]*[-a-z0-9+&@#\/%=~_|]/i",$website)) {
$websiteErr = '非法的 URL 地址';
$website = ''; //格式非法,将$website变量清空
}
第四关
$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;
}
来源地址:https://blog.csdn.net/m0_62788719/article/details/127657571
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341