备注
二进制安全字符串比较(区分大小):int strcmp(string $str1, string $str2)
注意:$str1小于$str2返回<0,如果$str1大于$str2返回>0,如果两者相等,返回0
strip_tags:从字符串中去除HTML和PHP标记
1.注册页面
<?php
$string = ‘qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM0123456789’;
$code = ”;
for ($i=1;$i<=4;$i++){
$code.='<span style="color:rgb(‘.mt_rand(0,255).’,’.mt_rand(0,255).’,’.mt_rand(0,255).’)">’.$string[mt_rand(0,strlen($string)-1)].'</span>’;
}
?>
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>注册页面</title>
</head>
<body>
<h1 align="center">笨猪注册页面</h1>
<form method="post" action="doAction.php">
<table border="1" cellpadding="0" cellspacing="0" width="50%" bgcolor="#ABCDEF" align="center">
<tr>
<td align="right">用户名</td>
<td><input type="text" name="username" placeholder="请输入合法用户名…"/>
</td>
</tr>
<tr>
<td align="right">密码</td>
<td><input type="password" name="password" placeholder="请输入密码…"/></td>
</tr>
<tr>
<td align="right">确认密码</td>
<td><input type="password" name="passwords" placeholder="请确认密码…"/></td>
</tr>
<tr>
<td align="right">邮箱</td>
<td><input type="text" name="email" placeholder="请输入合法的邮箱…"/></td>
</tr>
<tr>
<td align="right">兴趣爱好</td>
<td>
<input type="checkbox" name="fav[]" id="" value="PHP" />PHP
<input type="checkbox" name="fav[]" id="" value="JAVA" />JAVA
<input type="checkbox" name="fav[]" id="" value="PYTHON" />PYTHON
<input type="checkbox" name="fav[]" id="" value="GO" />GO
<input type="checkbox" name="fav[]" id="" value="Android" />Android
<input type="checkbox" name="fav[]" id="" value="C" />C
<input type="checkbox" name="fav[]" id="" value="C++" />C++
</td>
</tr>
<tr>
<td align="right">验证码</td>
<td><input type="text" name="verify" /><?PHP echo $code; ?>
<input type="hidden" name="verifys" value="<?PHP echo strip_tags($code); ?>"/>
</td>
</tr>
<tr>
<td colspan="2" align="center"><input type="submit" value="注册" /></td>
</tr>
</table>
</form>
</body>
</html>
2.处理页面
<?php
//防止中文乱码
header('content-type:text/html;charset=utf-8');
$username = $_POST['username'];
$password = $_POST['password'];
$passwords = $_POST['passwords'];
$email = $_POST['email'];
$fav = $_POST['fav'];
//统一转成小写进行比较
$verify = strtolower($_POST['verify']);
$verifys = strtolower($_POST['verifys']);
$redirectUrl = '<a href="register.php">重新注册</a>';
//判断用户名是否为空
if(strlen($username)==0){
exit('请输入用户名<br/>'.$redirectUrl);
}else{
//判断用户名是否为字母开头
$ascii = ord($username{0});
//检测ascii码是否在65-90(A-Z)或者97-122(a-z)
if(!(($ascii>=65&&$ascii<=90)||($ascii>=97&&$ascii<=122))){
exit('用户名首字母需要以字母开始<br/>'.$redirectUrl);
}
}
//判断密码是否为空
if(strlen($password)==0){
exit('请输入密码<br/>'.$redirectUrl);
}else{
if(strlen($password<6)){
exit('密码长度不符合规范<br/>'.$redirectUrl);
}
}
//确认密码是否为空
if(strlen($passwords)==0){
exit('请输入确认密码<br/>'.$redirectUrl);
}else{
if($password !== $passwords){
exit('两次输入的密码不一致<br/>'.$redirectUrl);
}
}
//判断邮箱是否为空
if(strlen($email) == 0){
exit('请输入邮箱<br/>'.$redirectUrl);
}else{
//判断是否有@
if(strpos($email,'@') == false){
exit('邮箱不合法<br/>'.$redirectUrl);
}
}
//验证码是否为空
if(strlen($verify) == 0){
exit('请输入验证码<br/>'.$redirectUrl);
}else{
//判断验证码是否正确
if($verify !== $verifys){
exit('验证码输入不正确<br/>'.$redirectUrl);
}
}
if(!empty($fav)){
//将数组转换成字符串
$favstr = join(',',$fav);
}
$passwordMd5 = md5($password);
echo '恭喜注册成功,用户信息如下:<br/>';
$userinfo = <<<EOF
<table align="center" border="1" cellspacing="0" cellpadding="0" width="50%">
<tr>
<td>用户名</td>
<td>密码</td>
<td>邮箱</td>
<td>兴趣爱好</td>
</tr>
<tr>
<td>{$username}</td>
<td>{$passwordMd5}</td>
<td>{$email}</td>
<td>{$favstr}</td>
</tr>
</table>
EOF;
echo $userinfo;
?>
效果图:
评论