php实现注册登录的完整程序(使用mysql数据库)

来源:互联网 发布:淘宝童装女孩模特 编辑:程序博客网 时间:2024/05/18 01:55

本文介绍下,用php与mysql数据库结合,实现的一个注册登录的完整代码,有需要的朋友,参考下吧。

实现用户注册登录的功能,共需要8个页面:
1,login.php 登陆页面
2,check.php  验证页面
3,mysql_connect.php数据库连接页面
4,ms_login.php 从数据库中提取数据验证
5,register.php 注册页面
6,register_1.php 是注册验证,并写入到数据库中
7,showing.php  图片验证码
8,huangying.html  是登陆成功的页面

下面我们逐一来实现。

1,login.php 登陆页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  <title>php登录页面_www.jbxue.com</title>  </head>  <style type="text/CSS">      .div      {          height:1000px;          width:700px;          text-align:center;          margin:20px;                }      .text      {                          }      .button      {          font-size:10px;                }        </style>  <body>  <form method="post" action="check.php">  <div class="div">      用户名<input type="text" name="name" >      密码:<input type="password" name="password">      <div class="button">      <input type="submit" value="提交">      <input type="reset" value="清除">      <a href="register.php" >注  册</a>  </div>  </div></form></body>  </html>

2,check.php 登录验证页面

<?php /*** 登录验证* edit www.jbxue.com*/require_once("ms_login.php");  //require_once ("mysql_connect.php");  $name=$_POST['name'];  $password=$_POST['password'];  if($name == "")  {    echo "请填写用户名<br>";    echo"<script type='text/javascript'>alert('请填写用户名');location='login.php'; </script>";  }  elseif($password == "")  {    //echo "请填写密码<br><a href='login.php'>返回</a>";    echo"<script type='text/javascript'>alert('请填写密码');location='login.php';</script>";      }  else  {      $colum=collect_data();     if(($colum['name'] == $name) && ($colum['password'] == $password))     {       //echo "验证成功!<br>";       echo"<script type='text/javascript'>alert('登陆成功');location='huanying.html';</script>";     }     else       //echo "密码错误<br>";       echo"<script type='text/javascript'>alert('密码错误');location='login.php';</script>";       //echo "<a href='login.php'>返回</a>";}  ?>

3,mysql_connect.php 数据库连接页面

<?php  // 连接服务器,并且选择test数据库  function(){        $db = mysql_connect("localhost","root","123")       or die("连接数据库失败!");    mysql_select_db("user")      or die ("不能连接到user".mysql_error());  }?>

4,ms_login.php 从数据库中提取数据验证

<?php  //从test数据库中提取数据  function collect_data(){  require_once ("mysql_connect.php");  $sql = "select * from user";  $result = mysql_query($sql);    $colum= mysql_fetch_array($result);  return colum;  }?>

5,register.php 注册页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  <title>php用户注册页面_www.jbxue.com</title>  </head>  <style type="text/css">      .div      {          height:1000px;          width:700px;          text-align:center;          margin:40px;                }      .text      {          font-size:20px;          margin:20px;                }      .button      {          font-size:10px;                }  </style>  <body>  <h1>注册页面</h1>  <form method="post" action="register_1.php">  <div class="div">      <div class="text">      用户名<input type="text" name="name" ></div>      <div class="text">      密码:<input type="password" name="password"></div>      <div class="text">      再次输入密码:<input type="password" name="pwd_again"></div>      <div class="text">      验证码:<input type="text" name="check"><img src="showimg.php"></img></div>            <div class="text">      <input type="radio" name="agree" value="是否同意我们的条款">同意我们的条款?</div>            <input type="submit" value="提交">      <input type="reset" value="清除">  </div>  </form></body>  </html>

6,register_1.php 是注册验证,并写入到数据库中

<?php  /*** 用户注册验证 写入数据库* edit www.jbxue.com*/require_once 'mysql_connect.php';  $name=$_POST['name'];  $password=$_POST['password'];  $pwd_again=$_POST['pwd_again'];  $code=$_POST['check'];  if($name==""|| $password=="")  {      echo"用户名或者密码不能为空";  }  else   {      if($password!=$pwd_again)      {          echo"两次输入的密码不一致,请重新输入!";          echo"<a href='register.php'>重新输入</a>";                }      else if($code!=$_SESSION['check'])      {          echo"验证码错误!";      }      else      {          $sql="insert into user values('105','$name','$password')";          $result=mysql_query($sql);          if(!$result)          {              echo"注册不成功!";              echo"<a href='register.php'>返回</a>";          }          else           {              echo"注册成功!";          }      }  }  ?>

7,showing.php  图片验证码

<?php  /*   网站验证码程序  *   运行环境: PHP5.0.18 下调试通过 *   需要 gd2 图形库支持(PHP.INI中 php_gd2.dll开启) *    */  //随机生成一个4位数的数字验证码  $num="";  for($i=0;$i<4;$i++){      $num .= rand(0,9);  }  //4位验证码也可以用rand(1000,9999)直接生成  //将生成的验证码写入session,备验证页面使用  Session_start();  $_SESSION["check"] = $num;  //创建图片,定义颜色值  Header("Content-type: image/PNG");  srand((double)microtime()*1000000);  $im = imagecreate(60,20);  $black = ImageColorAllocate($im, 0,0,0);  $gray = ImageColorAllocate($im, 200,200,200);  imagefill($im,0,0,$gray);    //随机绘制两条虚线,起干扰作用  $style = array($black, $black, $black, $black, $black, $gray, $gray, $gray, $gray, $gray);  imagesetstyle($im, $style);  $y1=rand(0,20);  $y2=rand(0,20);  $y3=rand(0,20);  $y4=rand(0,20);  imageline($im, 0, $y1, 60, $y3, IMG_COLOR_STYLED);  imageline($im, 0, $y2, 60, $y4, IMG_COLOR_STYLED);    //在画布上随机生成大量黑点,起干扰作用;  for($i=0;$i<80;$i++)  {  imagesetpixel($im, rand(0,60), rand(0,20), $black);  }  //将四个数字随机显示在画布上,字符的水平间距和位置都按一定波动范围随机生成  $strx=rand(3,8);  for($i=0;$i<4;$i++){  $strpos=rand(1,6);      imagestring($im,5,$strx,$strpos, substr($num,$i,1), $black);      $strx+=rand(8,12);  }  ImagePNG($im);  ImageDestroy($im);  ?>

有关php验证码的内容,大家还可以参考:
一个php验证码的封装类
php自定义大小验证码的实例代码
php生成扭曲及旋转的验证码图片的实例代码
php仿QQ验证码的实现代码
php5验证码类(简易实用型)
php验证码(GD库生成验证码)的例子
php GD库生成验证码的实例
一个比较稳定的php登陆系统验证码
用php生成带有雪花背景的验证码

8,huangying.html  登陆成功的页面

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">  <html>  <head>  <meta http-equiv="Content-Type" content="text/html; charset=gb2312">  <title>欢迎光临_www.jbxue.com</title>  </head>  <body>  <h1>脚本学堂,欢迎您的光临。</h1>  </body>  </html>
1 0
原创粉丝点击