
正文
PHP之cookies小练习
提示:扫一扫查出行【扫一扫了解最新限行尾号】
复制提示
//5-1.php
1 <?
error_reporting(E_ALL ^ E_NOTICE);
if ($_COOKIE['username']!="") {
echo "欢迎!"."<br/>";
echo "用户名:".$_COOKIE['username']."<br/>";
echo "电话:".$_COOKIE['phone']."<br/>";
} else{
echo '
<html>
<head>
<title>用户注册</title>
</head>
<body>
<form method="post" action="5-2.php">
<p><font size="18">用户注册</p>
<table border="1" > <tr><td>用户名:</td>
<td><input name="xm" type="text" size="12"></td>
<td><font color="red">*6-12个字符(数字字母下划线)</font></td>
</tr> <tr><td>密码:</td>
<td><input name="pwd1" type="password" size="16"></td>
<td><font color="red">*6-16个数字</font></td>
</tr> <tr><td>确认密码:</td>
<td><input name="pwd2" type="password" size="16"></td>
<td><font color="red">*必须和密码一致</font></td>
</tr> <tr><td>电话:</td>
<td><input name="phone" type="text" size="11"></td>
<td><font color="red">*11位数字,第一位为1</font></td>
</tr> <tr><td colspan="3"><input type="submit" value="注册">
<input type="reset" value="取消">
</td>
</tr> </table>
</form>
</body>
</html>';}
?>
//5-2.php
1 <?
if( strlen($_POST["xm"])<6 || strlen($_POST["xm"])>12 ){
echo "<script>alert('用户名长度错误');location.href='5-1.php';</script>";
}
else if ( strpos($_POST["xm"], "_")==false) {
echo "<script>alert('用户名中应存在下划线');location.href='5-1.php';</script>";
}
else if ( preg_match('/[a-zA-Z]/', $_POST["xm"])==false) {
echo "<script>alert('用户名中应存在英文字母');location.href='5-1.php';</script>";
}
else if ( preg_match('/[0-9]/', $_POST["xm"])==false) {
echo "<script>alert('用户名中应存在数字');location.href='5-1.php';</script>";
}
else if ( strlen($_POST["pwd1"])<6 || strlen($_POST["pwd1"])>16 ) {
echo "<script>alert('密码长度错误');location.href='5-1.php';</script>";
}
else if ($_POST["pwd1"]!=$_POST["pwd2"]) {
echo "<script>alert('两次密码不一致');location.href='5-1.php';</script>";
}
else if (strlen($_POST["phone"])!=11) {
echo "<script>alert('电话长度错误');location.href='5-1.php';</script>";
}
else if (substr($_POST["phone"],0,1)!="1") {
echo "<script>alert('电话第一位应该为1');location.href='5-1.php';</script>";
}
else if ( strpos($_POST["phone"], "_")) {
echo "<script>alert('电话中不应该存在下划线');location.href='5-1.php';</script>";
}
else if ( preg_match('/[a-zA-Z]/', $_POST["phone"])==true) {
echo "<script>alert('电话中不应该存在英文字母');location.href='5-1.php';</script>";
}
else{
setcookie("username",$_POST["xm"],time()+600);
setcookie("password",$_POST["pwd1"],time()+600);
setcookie("phone",$_POST["phone"],time()+600);
echo "注册成功"."<br/>";
echo "用户名:".$_POST["xm"]."<br/>";
echo "电话:".$_POST["phone"]."<br/>";
}
?>








