无为清净楼资源网 Design By www.qnjia.com
本文实例讲述了PHP实现的用户注册表单验证功能。分享给大家供大家参考,具体如下:
注册界面
register.html
<h1>用户注册</h1> <form method="post" action="register_verify.php"> <input type="text" placeholder="用户名" name="username"><br><br> <input type="password" placeholder="密码" name="password"><br><br> <input type="password" placeholder="重复密码" name="password2"><br><br> <label>性别: <input type="radio" name="sex" value="男" checked="checked">男 <input type="radio" name="sex" value="女">女</label><br><br> <input type="email" placeholder="邮箱" name="email"><br><br> <button class="btn" type="submit">注册</button> </form>
register_verify.php
<"mysql.php"; //导入mysql.php访问数据库 $username=$_POST['username']; $password=$_POST['password']; $password2=$_POST['password2']; $sex=$_POST['sex']; $email=$_POST['email']; if(checkEmpty($username,$password,$password2,$sex,$email)){ if(checkpwd($password,$password2)){ if(checkEmail($email)){ if(insert($username,$password,$sex,$email)) echo"注册成功"; } } } //方法:判断是否为空 function checkEmpty($username,$password,$password2,$sex,$email){ if($username==null||$password==null||$password2==null){ echo '<html><head><Script Language="JavaScript">alert("用户名或密码为空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } else{ if($sex==null){ echo '<html><head><Script Language="JavaScript">alert("性别为空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } elseif($email==null){ echo '<html><head><Script Language="JavaScript">alert("邮箱为空");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } else{ return true; } } } //方法:检查两次密码是否相同 function checkpwd($password,$password2){ if($password==$password2) return true; else echo '<html><head><Script Language="JavaScript">alert("两次密码不一致");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } //方法:邮箱格式验证 function checkEmail($email){ $preg = '/^(\w{1,25})@(\w{1,16})(\.(\w{1,4})){1,3}$/'; if(preg_match($preg, $email)){ return true; }else{ echo '<html><head><Script Language="JavaScript">alert("邮箱格式有误");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } } //方法:将数据插入数据库中 function insert($username,$password,$sex,$email){ $conn=new Mysql(); $sql="insert into user VALUE (null,'$username','$password','$sex','$email')"; $result=$conn->sql($sql); if($result){ return true; } else{ echo '<html><head><Script Language="JavaScript">alert("写入数据库失败");</Script></head></html>' . "<meta http-equiv=\"refresh\" content=\"0;url=register.html\">"; } $conn->close(); }
验证码和数据库的实现方法前面写过,这里不再赘述。
可参考前面两篇文章:
PHP封装mysqli基于面向对象的mysql数据库操作类
PHP通过GD库实现验证码功能
PS:这里再为大家提供2款非常方便的正则表达式工具供大家参考使用:
JavaScript正则表达式在线测试工具:
http://tools.jb51.net/regex/javascript
正则表达式在线生成工具:
http://tools.jb51.net/regex/create_reg
更多关于PHP相关内容感兴趣的读者可查看本站专题:《php程序设计安全教程》、《php安全过滤技巧总结》、《PHP基本语法入门教程》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》
希望本文所述对大家PHP程序设计有所帮助。
标签:
PHP,用户注册,表单验证
无为清净楼资源网 Design By www.qnjia.com
广告合作:本站广告合作请联系QQ:858582 申请时备注:广告合作(否则不回)
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
免责声明:本站文章均来自网站采集或用户投稿,网站不提供任何软件下载或自行开发的软件! 如有用户或公司发现本站内容信息存在侵权行为,请邮件告知! 858582#qq.com
无为清净楼资源网 Design By www.qnjia.com
暂无评论...
更新日志
2024年09月25日
2024年09月25日
- 杨千嬅.2002-万紫千红演唱会2CD(2024环球红馆40复刻系列)【环球】【WAV+CUE】
- 郭采洁.2015-Begin.Again爱造飞鸡【华纳】【FLAC分轨】
- 许志安.2011-ON.AND.ON【东亚】【WAV+CUE】
- 潘秀琼.1994-木兰从军【名将】【WAV+CUE】
- 纯音入心系列纯音乐《古筝系列-当流行乐遇到古筝》1CD[MP3][1.9GB]
- 纯音入心系列纯音乐《精选古筝名曲100首》1CD[MP3][388.7MB]
- 纯音入心系列纯音乐《天籁古筝》1CD[MP3][331MB]
- 男女对唱典藏天碟《发烧对唱·那个季节里的歌DSD》10CD[WAV]
- 群星2010-歌林精选辑[歌林][WAV+CUE]
- TrioZimbalist-PianoTriosofWeinberg,Auerbach,Dvorak(2024)[24-44,1]wav
- 群星.1992-滚石第一流台湾歌·12王牌大车拼【滚石】【WAV+CUE】
- 林子祥.1984-创作歌集【华纳】【WAV+CUE】
- 周汤豪.2010-周汤豪【华纳】【FLAC分轨】
- Mozart-TheStringQuintets-AmadeusQuartet,CecilAronowitz(2017)[24-44,1][WAV+CUE]
- JamesWilliamsDennisIrwin-Focus(2024,Red)[24-48]FLAC