前端学习笔记-4.2php实现注册功能

前提是php+apache环境已经搭建好
思路:会用以下函数和ifelse条件语句
实现登录需要的函数:
1.mt_rand():从0-255之间产生1个随机数;

2.strip_tags( s t r i n g [ , string[, allowtags]):过滤HTML标记

s t r i n g = < h 1 > t h i s i s a t e s t < / h 1 > ; e c h o s t r i p t a g s ( string='<h1>this is a test</h1>'; echo strip_tags( string);//this is a test

3.trim( s t r i n g [ , string[, charList]):默认去掉字符串两端的空格,也可以去掉指定字符串
ltrim( s t r i n g [ , string[, charList]):去掉左边
rtrim( s t r i n g [ , string[, charList]):去掉右边

例子:
s t r i n g = a b c d b c a ; e c h o t r i m ( string='abcdbca'; echo trim( string,‘a’);//bcdbc

4.strtolower( s t r i n g ) : s t r t o u p p e r ( string):返回小写的字符串 strtoupper( string):返回大写之后的字符串

例子:
s t r i n g = h E E L L d d W W ; e c h o s t r t o l o w e r ( string='hEELL ddWW'; echo strtolower( string);//heell ddww
echo strtoupper($string);//HEELL DDWW

5.exit终止函数
echo ‘111’;
exit;//程序终止执行了
echo ‘222’;//看不到222

两个页面:
1.reg.php:静态页及验证码的实现
2.doReg:功能实现代码页
这是reg.php页面:

<?php
$string='1234567890qwertyuiopasdfghjklmnbvcxzQWERTYUIOPLKJHGFDSAZXCVBNM';
$code='<span style="color:rgb('.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).')">'.$string{mt_rand(0,strlen($string)-1)}.'</span>';
$code.='<span style="color:rgb('.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).')">'.$string{mt_rand(0,strlen($string)-1)}.'</span>';
$code.='<span style="color:rgb('.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).')">'.$string{mt_rand(0,strlen($string)-1)}.'</span>';
$code.='<span style="color:rgb('.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).','.mt_rand(0,255).')">'.$string{mt_rand(0,strlen($string)-1)}.'</span>';
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
	<meta http-equiv="Content-Type" content="text/html;charset=UTF-8" />
	<title>laocunzhang注册页面</title>
</head>
<body>
	<h2>老村长注册页面</h2>
	<form action="doReg.php" method='post'>
		<table border='1' cellpadding='0' cellspacing='0' bgcolor='#ABCDEF' width='80%'>
			<tr>
				<td align='right'>用户名</td>
				<td><input type="text" name="username" id="" placeholder="请输入用户名"></td>
			</tr>
			<tr>
				<td align='right'>密码</td>
				<td><input type="password" name="password" id="" placeholder="请输入密码"></td>
			</tr>
			<tr>
				<td align='right'>确认密码</td>
				<td><input type="password" name="password1" id="" placeholder="请确认密码"></td>
			</tr>
			<tr>
				<td align='right'>邮箱</td>
				<td><input type="email" name="email" id=""></td>
			</tr>
			<tr>
				<td align='right'>年龄</td>
				<td><input type="number" name="age" id=""></td>
			</tr>
			<tr>
				<td align='right'>验证码</td>
				<td><input type="text" name="verify" id=""/><?php echo $code;?>
					<input type="hidden" name="verify1" value='<?php echo strtolower(strip_tags($code));?>'/>
				</td>
			</tr>
			<tr>
				<td colspan='2' align="center"><input type="submit" name="sub" id=""></td>
			</tr>
		</table>
	</form>
</body>
</html>

这是doReg页面:

<?php
//接收信息
$username=$_POST['username'];
$password=$_POST['password'];
$password1=$_POST['password1'];
$email=$_POST['email'];
$age=$_POST['age'];
$verify=$_POST['verify'];
$verify1=$_POST['verify1'];
// echo $verify,'-',$verify1;exit;

// echo $username,'-',$password,'-',$password1,'-',$email,'-',$age,'-',$verify,'-',$verify1;
//1.判断下用户名首字母是否符合规范
$char=$username{0};
$ascii=ord($char);
if(($ascii>=97 && ascii<=122)||($ascii>=65 && $ascii<=90)){
    //2.判断用户名长度是否符合规范
    $uesrLen=strlen($username);
    if($uesrLen>=5 && $uesrLen<=10){
        //3.判断密码是否为空
        $pwdLen=strlen($password);
        if($pwdLen>0){
            //4.判断两次密码一致
            if($password===$password1){
                //5.判断邮箱的合法性
                if(strpos($email,'@')!==false){
                    //6.验证年龄是否符合规范
                    if($age>=1 &&$age<=125){
                        //7.校验验证码是否一致
                        if($verify===$verify1){
                            echo '恭喜您'.$userLen.'注册成功<br/>';
                            echo '注册信息如下:<br/>';
                            $userInfo=<<<EOF
                            <table border='1' cellpadding='0' cellspacing='0' width='60%' bgcolor='pink'>
                                <tr>
                                    <td>用户名</td>
                                    <td>{$username}</td>
                                </tr>
                                <tr>
                                    <td>密码</td>
                                    <td>{$password}</td>
                                </tr>
                                <tr>
                                    <td>邮箱</td>
                                    <td>{$email}</td>
                                </tr>
                                <tr>
                                    <td>年龄</td>
                                    <td>{$age}</td>
                                </tr>
                            </table>
EOF;
                            echo $userInfo;
                            echo '3秒钟之后跳转到百度页面<br/>';
                            echo '<meta http-equiv="refresh" content="3;url=http://www.baidu.com/"/>';

                        }else{
                            echo '两次验证码不一致<br/>';
                        }
                    }else{
                        echo '年龄不符合规则<br/>';
                    }
                }else{
                    echo '{$email}不合法<br/>';
                }
            }else{
                echo '两次密码不一致<br/>';
            }
        }else{
            echo '密码不能为空<br/>';
        }
    }else{
        echo '{$username}长度不符合规则<br/>';
    }
}else{
    echo '{$username}用户名不是以字母开始<br/>';
}

猜你喜欢

转载自blog.csdn.net/qq_43000359/article/details/82763723