PHP实现注册登录功能完整教程及代码 含验证码

版权声明:大量计算机类书籍低价出售 QQ群:832971163 或者直接访问:https://j.youzan.com/eNosSY https://blog.csdn.net/Momentyol/article/details/50075031
PHP实现注册登录功能完整教程及代码    (含验证码)
        只是为实现功能,所以页面并没有美化,本教程包含注册登录功能的完整流程及代码:
文件列表如图:

注册html代码:
<!DOCTYPE HTML>
<html> 
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<link rel="stylesheet" type="text/css" href="css/register.css" />
<title>注册界面</title>
</head>


<body>  
<span style="white-space:pre">	</span>  <form  name="form1"  method="post" action="add.php">
<span style="white-space:pre">	</span>  <div>
<span style="white-space:pre">	</span>      <ul>
<span style="white-space:pre">		</span>      <li><input name="nickname" type="text"  placeholder ="昵称:数字、字母、下划线"/></li>
<span style="white-space:pre">			</span>  <li><input name="studentid" type="text" placeholder ="学号:9位纯数字"/></li>
<span style="white-space:pre">		</span>      <li><input name="password" type="password" placeholder ="密码:至少6位"/></li>
<span style="white-space:pre">		</span>      <li><input name="confirm" type="password"  placeholder ="确认密码"/></li>
<span style="white-space:pre">		</span>      <li><input name="emailaddress" type="text"  placeholder ="常用邮箱"/></li>
<span style="white-space:pre">			</span>  <li><input name="verification" type="text"  placeholder ="输入验证码"/></li>
<span style="white-space:pre">		</span>  </ul>
<span style="white-space:pre">			</span>  <div id="text11"><img name="validate" onclick="validate.src+='?' + Math.random();" 
<span style="white-space:pre">			</span>  src="verification.php"  alt="点击刷新">
<span style="white-space:pre">			</span>  </div><span style="white-space:pre">	</span>  
<span style="white-space:pre">		</span>     <input type="reset" value="重置" />
<span style="white-space:pre">			</span>  <input type="submit" value="确定" />  
<span style="white-space:pre">		</span>    </div>
<span style="white-space:pre">		</span>  
<span style="white-space:pre">	</span>  </form><span style="white-space:pre">	</span>  
   </div> 
</body>
</html>
效果图如下:

可以看出生成了验证码,且验证码可以点击刷新,刷新页面时也可以刷新
验证码文件verification.php:
<?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
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('set names utf8_bin');
?>
根据本机的数据库密码进行配置  命名为connect.php

<?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 * from student_information where studentid = '$studentid'";
   $today=date('Y-m-d H: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= "insert into student_information(nickname,studentid,password,confirm,emailaddress,registrationtime,logintime)values('$nickname','$studentid','$password','$confirm','$emailaddress','$today','$today')";
	           //插入数据库
                  if(!(mysql_query($insertsql))){
	                  echo mysql_error();
                    }else{
	                  echo "<script>alert('注册成功!去登陆');window.location.href='login.html'</script>";
                             }
		        } 
?>

session主要用来核对验证码,在验证码文件里用session存储验证码数字,在这里核对是否填写正确。
require  包含配置链接文件
post接收数据,并存储在变量中。
用正则表达式验证填写的信息是否合乎规范以及是否已经存在用户,若所有信息核对无误,写入数据库
注释已经很清楚,不做赘述

登录界面代码login.html:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html>
<head>
<title>登陆</title>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312">
</head>
<form action="login.php" method="post">
  <div>
    <ul>
          <li><input name="nickname" type="text"  placeholder ="昵称"/></li>
          <li><input name="studentid" type="text"  placeholder ="学号"/></li>
          <li><input name="password" type="password"  placeholder ="密码"/></li>
          <li><input name="verification" type="text"  placeholder ="验证码"/></li>
          <input name="reset" type="reset"  value="重置"/>
         <input name="submit" type="button"  value="登录"/>
        </ul>
       <div id="text11"><img name="validate" onclick="validate.src+='?' + Math.random();" 
        src="verification.php"  alt="点击刷新">
        </div>
</div>


</form>
</body>
</html>
效果图如下:

验证码效果和上边相同,不做赘述
接下来是登录验证php代码(login.php):
<?php
    session_start();
    require_once('connect.php');//连接数据库
   $nickname=$_POST['nickname'];
   $password=$_POST['password'];
   $verification=$_POST['verification'];
   $studentid=$_POST['studentid'];
   
    $today=date('Y-m-d H:i:s');//获取时间作为本次登录时间
    $ip=$_SERVER["REMOTE_ADDR"];//获取登陆IP地址
	
    $sql="select * from student_information where nickname='{$nickname}' and password='{$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="UPDATE student_information SET logintime='$today' WHERE studentid='$studentid'";
		    $ip="UPDATE student_information SET ip='$ip' WHERE studentid='$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>"; 
   
	   }
	   
?>
详细代码文件压缩包已上传  至我的资源  点击下载完整教程及代码包


没有更多推荐了,返回首页