版权声明:本文为博主原创文章,转载请注明出处。转自 http://blog.csdn.net/momentyol
PHP实现注册登录功能完整教程及代码 (含验证码) 只是为实现功能,所以页面并没有美化,本教程包含注册登录功能的完整流程及代码:文件列表如图:
注册html代码:[html]viewplaincopyprint?
- <!DOCTYPEHTML>
- <html>
- <head>
- <metahttp-equiv="Content-Type"content="text/html;charset=utf-8"/>
- <linkrel="stylesheet"type="text/css"href="css/register.css"/>
- <title>注册界面</title>
- </head>
- <body>
- <spanstyle="white-space:pre"></span><formname="form1"method="post"action="add.php">
- <spanstyle="white-space:pre"></span><div>
- <spanstyle="white-space:pre"></span><ul>
- <spanstyle="white-space:pre"></span><li><inputname="nickname"type="text"placeholder="昵称:数字、字母、下划线"/></li>
- <spanstyle="white-space:pre"></span><li><inputname="studentid"type="text"placeholder="学号:9位纯数字"/></li>
- <spanstyle="white-space:pre"></span><li><inputname="password"type="password"placeholder="密码:至少6位"/></li>
- <spanstyle="white-space:pre"></span><li><inputname="confirm"type="password"placeholder="确认密码"/></li>
- <spanstyle="white-space:pre"></span><li><inputname="emailaddress"type="text"placeholder="常用邮箱"/></li>
- <spanstyle="white-space:pre"></span><li><inputname="verification"type="text"placeholder="输入验证码"/></li>
- <spanstyle="white-space:pre"></span></ul>
- <spanstyle="white-space:pre"></span><divid="text11"><imgname="validate"onclick="validate.src+=\’?\’+Math.random();"
- <spanstyle="white-space:pre"></span>src="verification.php"alt="点击刷新">
- <spanstyle="white-space:pre"></span></div><spanstyle="white-space:pre"></span>
- <spanstyle="white-space:pre"></span><inputtype="reset"value="重置"/>
- <spanstyle="white-space:pre"></span><inputtype="submit"value="确定"/>
- <spanstyle="white-space:pre"></span></div>
- <spanstyle="white-space:pre"></span>
- <spanstyle="white-space:pre"></span></form><spanstyle="white-space:pre"></span>
- </div>
- </body>
- </html>
效果图如下:
可以看出生成了验证码,且验证码可以点击刷新,刷新页面时也可以刷新验证码文件verification.php:[php]viewplaincopyprint?
- <?php
- session_start();
- $img=imagecreatetruecolor(100,35);
- $black=imagecolorallocate($img,0x00,0x00,0x00);
- $green=imagecolorallocate($img,0x00,0xFF,0x00);
- $white=imagecolorallocate($img,0xFF,0xFF,0xFF);
- imagefill($img,0,0,$white);
- //生成随机的验证码
- $code=\’\’;
- for($i=0;$i<4;$i++){
- $code.=rand(0,9);
- }
- $_SESSION[\’rand\’]=$code;//存储验证码
- imagestring($img,30,28,10,$code,$black);
- //加入噪点干扰
- for($i=0;$i<200;$i++){
- imagesetpixel($img,rand(0,100),rand(0,100),$black);
- imagesetpixel($img,rand(0,100),rand(0,100),$green);
- }
- //输出验证码
- header("content-type:image/png");
- imagepng($img);
- imagedestroy($img);
- ?>
生成4位随机数字并加入噪点干扰,防止机器识别。并用session存储生成的数字,用于后边验证。
php代码通过接收前台数据进行验证先写php的配置文件代码:[php]viewplaincopyprint?
- <?php
- header("Content-type:text/html;charset=utf8_bin");
- define(\’HOST\’,\’127.0.0.1\’);
- define(\’USERNAME\’,\’root\’);
- define(\’PASSWORD\’,\’root\’);
- //连库
- $con=mysql_connect(HOST,USERNAME,PASSWORD);
- //选库
- mysql_select_db(\’rrf\’);
- //字符集
- mysql_query(\’setnamesutf8_bin\’);
- ?>
根据本机的数据库密码进行配置 命名为connect.php
[php]viewplaincopyprint?
- <?php
- //把传递过来的信息入库;
- session_start();
- //用于核对验证码
- require_once(\’connect.php\’);
- //print_r($_POST);
- $nickname=$_POST["nickname"];
- $studentid=$_POST[\’studentid\’];
- $password=$_POST[\’password\’];
- $confirm=$_POST[\’confirm\’];
- $emailaddress=$_POST[\’emailaddress\’];
- $verification=$_POST["verification"];
- $sql="select*fromstudent_informationwherestudentid=\’$studentid\’";
- $today=date(\’Y-m-dH:i:s\’);//获取时间作为注册时间
- $query=mysql_query($sql);
- $rows=mysql_num_rows($query);
- //验证填写信息是否合乎规范
- if(empty($nickname)||empty($studentid)||empty($password)||empty($confirm)||empty($emailaddress)||empty($emailaddress)){
- echo"<script>alert(\’信息不能为空!重新填写\’);window.location.href=\’register.html\'</script>";
- }elseif((strlen($nickname)<4)||(!preg_match(\’/^\\w+$/i\’,$nickname))){
- echo"<script>alert(\’用户名至少4位且不含非法字符!重新填写\’);window.location.href=\’register.html\'</script>";
- //判断用户名长度
- }elseif((strlen($studentid)!=9)||(!(ctype_digit($studentid)))){
- echo"<script>alert(\’学号为9位纯数字!重新填写\’);window.location.href=\’register.html\'</script>";
- //判断学号是否填写正确
- }elseif($rows>0){
- echo"<script>alert(\’此学号已经注册!重新填写\’);window.location.href=\’register.html\'</script>";
- //学号不能重复
- }elseif(strlen($password)<6){
- echo"<script>alert(\’密码至少6位!重新填写\’);window.location.href=\’register.html\'</script>";
- //判断密码长度
- }elseif($password!=$confirm){
- echo"<script>alert(\’两次密码不相同!重新填写\’);window.location.href=\’register.html\'</script>";
- //检测两次输入密码是否相同
- }elseif(!preg_match(\’/^[\\w\\.]+@\\w+\\.\\w+$/i\’,$emailaddress)){
- echo"<script>alert(\’邮箱不合法!重新填写\’);window.location.href=\’register.html\'</script>";
- //判断邮箱格式是否合法
- }//elseif($verification!=$_SESSION[\’$code\’]){
- elseif(($_SESSION[\’rand\’])!=($verification)){
- echo"<script>alert(\’验证码错误!重新填写\’);window.location.href=\’register.html\'</script>";
- //判断验证码是否填写正确
- }else{
- $insertsql="insertintostudent_information(nickname,studentid,password,confirm,emailaddress,registrationtime,logintime)values(\’$nickname\’,\’$studentid\’,\’$password\’,\’$confirm\’,\’$emailaddress\’,\’$today\’,\’$today\’)";
- //插入数据库
- if(!(mysql_query($insertsql))){
- echomysql_error();
- }else{
- echo"<script>alert(\’注册成功!去登陆\’);window.location.href=\’login.html\'</script>";
- }
- }
- ?>
session主要用来核对验证码,在验证码文件里用session存储验证码数字,在这里核对是否填写正确。require 包含配置链接文件post接收数据,并存储在变量中。
用正则表达式验证填写的信息是否合乎规范以及是否已经存在用户,若所有信息核对无误,写入数据库注释已经很清楚,不做赘述
登录界面代码login.html:[html]viewplaincopyprint?
- <!DOCTYPEhtmlPUBLIC"-//W3C//DTDXHTML1.0Transitional//EN""http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
- <html>
- <head>
- <title>登陆</title>
- <metahttp-equiv="Content-Type"content="text/html;charset=gb2312">
- </head>
- <formaction="login.php"method="post">
- <div>
- <ul>
- <li><inputname="nickname"type="text"placeholder="昵称"/></li>
- <li><inputname="studentid"type="text"placeholder="学号"/></li>
- <li><inputname="password"type="password"placeholder="密码"/></li>
- <li><inputname="verification"type="text"placeholder="验证码"/></li>
- <inputname="reset"type="reset"value="重置"/>
- <inputname="submit"type="button"value="登录"/>
- </ul>
- <divid="text11"><imgname="validate"onclick="validate.src+=\’?\’+Math.random();"
- src="verification.php"alt="点击刷新">
- </div>
- </div>
- </form>
- </body>
- </html>
效果图如下:
验证码效果和上边相同,不做赘述接下来是登录验证php代码(login.php):[php]viewplaincopyprint?
- <?php
- session_start();
- require_once(\’connect.php\’);//连接数据库
- $nickname=$_POST[\’nickname\’];
- $password=$_POST[\’password\’];
- $verification=$_POST[\’verification\’];
- $studentid=$_POST[\’studentid\’];
- $today=date(\’Y-m-dH:i:s\’);//获取时间作为本次登录时间
- $ip=$_SERVER["REMOTE_ADDR"];//获取登陆IP地址
- $sql="select*fromstudent_informationwherenickname=\'{$nickname}\’andpassword=\'{$password}\’";
- $rst=mysql_query($sql);
- $row=mysql_fetch_assoc($rst);
- //验证登录信息是否正确
- if(($_SESSION[\’rand\’])!=($verification)){
- echo"<script>alert(\’验证码错误!重新填写\’);window.location.href=\’login.html\'</script>";
- //判断验证码是否填写正确
- }elseif($row){
- setcookie(\’niacname\’,$nickname,time()+10,\’/\’);
- setcookie(\’studentid\’,$studentid,time()+10,\’/\’);
- $up="UPDATEstudent_informationSETlogintime=\’$today\’WHEREstudentid=\’$studentid\’";
- $ip="UPDATEstudent_informationSETip=\’$ip\’WHEREstudentid=\’$studentid\’";
- if(mysql_query($up)){
- }
- if(mysql_query($ip)){
- }
- //更新登录时间及ip
- echo"<script>alert(\’登陆成功!欢迎\’);window.location.href=\’content.php\'</script>";
- }else{
- echo"<script>alert(\’登陆信息有误!重新填写\’);window.location.href=\’login.html\'</script>";
- }
- ?>