基于PHP实现用户注册登录功能


Posted in PHP onOctober 14, 2016

本文介绍的是基于PHP实现用户注册登录功能,本项目分为四部分内容:1前端页面制作,2验证码制作,3实现注册登陆,4功能完善。具体情况可以往下看。

验证码制作

一、实验简介

本次实验将会带领大家使用面向对象的思想封装一个验证码类。并在注册和登陆界面展示使用。通过本次实验的学习,你将会领悟到 PHP 的 OOP 思想,以及 GD 库的使用,验证码生成。

1.1 涉及到的知识点

  • PHP
  • GD库
  • OOP编程

1.2 开发工具

sublime,一个方便快速的文本编辑器。点击桌面左下角: 应用程序菜单/开发/sublime

二、封装验证码类

2.1 建立目录以及准备字体

在 web 目录下建立一个 admin 目录作为我们的后台目录,存放后台代码文件。在 admin 下建立一个 fonts 目录,用于存放制作验证码所需字体。

在 admin 下新建一个 Captcha.php 文件,这就是我们需要编辑的验证码类文件。

当前目录层次结构:
基于PHP实现用户注册登录功能

编辑 Captcha.php 文件:

<?php 
/**
* Captcha class
*/
class Captcha
{
  
  function __construct()
  {
    # code...
  }
}

添加该类的私有属性和构造方法:

<?php 
/**
* Captcha class
*/
class Captcha
{
  private $codeNum;  //验证码位数
  private $width;  //验证码图片宽度
  private $height;  //验证码图片高度
  private $img;  //图像资源句柄
  private $lineFlag;  //是否生成干扰线条
  private $piexFlag;  //是否生成干扰点
  private $fontSize;  //字体大小
  private $code;  //验证码字符
  private $string;  //生成验证码的字符集
  private $font;  //字体
  function __construct($codeNum = 4,$height = 50,$width = 150,$fontSize = 20,$lineFlag = true,$piexFlag = true)
  {
    $this->string = 'qwertyupmkjnhbgvfcdsxa123456789';  //去除一些相近的字符
    $this->codeNum = $codeNum;
    $this->height = $height;
    $this->width = $width;
    $this->lineFlag = $lineFlag;
    $this->piexFlag = $piexFlag;
    $this->font = dirname(__FILE__).'/fonts/consola.ttf';
    $this->fontSize = $fontSize;
  }
}

字体文件可通过以下命令下载到 fonts 目录:

$ wget http://labfile.oss.aliyuncs.com/courses/587/consola.ttf

接下来开始编写具体的方法:

创建图像资源句柄

//创建图像资源  
public function createImage(){
    $this->img = imagecreate($this->width, $this->height);  //创建图像资源
    imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));  //填充图像背景(使用浅色)
  }

用到的相关函数

  • imagecreate:新建一个基于调色板的图像
  • imagecolorallocate:为一幅图像分配颜色
  • mt_rand:生成更好的随机数

创建验证码字符串并输出到图像

//创建验证码  
public function createCode(){
    $strlen = strlen($this->string)-1;
    for ($i=0; $i < $this->codeNum; $i++) { 
      $this->code .= $this->string[mt_rand(0,$strlen)];  //从字符集中随机取出四个字符拼接
    }
     $_SESSION['code'] = $this->code;  //加入 session 中
  
   //计算每个字符间距
    $diff = $this->width/$this->codeNum;
    for ($i=0; $i < $this->codeNum; $i++) { 
          //为每个字符生成颜色(使用深色)
     $txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
     //写入图像
      imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
    }
  }

用到的相关函数

  • imagecreate:新建一个基于调色板的图像
  • imagecolorallocate:为一幅图像分配颜色
  • mt_rand:生成更好的随机数

创建验证码字符串并输出到图像

//创建验证码  
public function createCode(){
    $strlen = strlen($this->string)-1;
    for ($i=0; $i < $this->codeNum; $i++) { 
      $this->code .= $this->string[mt_rand(0,$strlen)];  //从字符集中随机取出四个字符拼接
    }
     $_SESSION['code'] = $this->code;  //加入 session 中
  
   //计算每个字符间距
    $diff = $this->width/$this->codeNum;
    for ($i=0; $i < $this->codeNum; $i++) { 
          //为每个字符生成颜色(使用深色)
     $txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
     //写入图像
      imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
    }
  }

用到的相关函数:

  • imagettftext:用 TrueType 字体向图像写入文本

创建干扰线条

//创建干扰线条(默认四条)
public function createLines(){
    for ($i=0; $i < 4; $i++) { 
      $color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155));  //使用浅色
      imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color); 
    }
  }

用到的相关函数:

  • imageline:画一条线段

创建干扰点

//创建干扰点 (默认一百个点)
public function createPiex(){
    for ($i=0; $i < 100; $i++) { 
      $color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
      imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
    }
  }

使用的相关函数:

  • imagesetpixel:画一个单一像素

对外输出图像:
   

public function show()
  {
    $this->createImage();
    $this->createCode();
    if ($this->lineFlag) {  //是否创建干扰线条
      $this->createLines();
    }
    if ($this->piexFlag) {  //是否创建干扰点
      $this->createPiex();
    }
    header('Content-type:image/png');  //请求页面的内容是png格式的图像
    imagepng($this->img);  //以png格式输出图像
    imagedestroy($this->img);  //清除图像资源,释放内存
  }

用到的相关函数:

  • imagepng:以 PNG 格式将图像输出到浏览器或文件
  • imagedestroy:销毁一图像

对外提供验证码:

public function getCode(){
    return $this->code;
  }
完整代码如下:
<?php 
/**
* Captcha class
*/
class Captcha
{
  private $codeNum;
  private $width;
  private $height;
  private $img;
  private $lineFlag;
  private $piexFlag;
  private $fontSize;
  private $code;
  private $string;
  private $font;
  function __construct($codeNum = 4,$height = 50,$width = 150,$fontSize = 20,$lineFlag = true,$piexFlag = true)
  {
    $this->string = 'qwertyupmkjnhbgvfcdsxa123456789';
    $this->codeNum = $codeNum;
    $this->height = $height;
    $this->width = $width;
    $this->lineFlag = $lineFlag;
    $this->piexFlag = $piexFlag;
    $this->font = dirname(__FILE__).'/fonts/consola.ttf';
    $this->fontSize = $fontSize;
  }

  public function createImage(){
    $this->img = imagecreate($this->width, $this->height);
    imagecolorallocate($this->img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));
  }

  public function createCode(){
    $strlen = strlen($this->string)-1;
    for ($i=0; $i < $this->codeNum; $i++) { 
      $this->code .= $this->string[mt_rand(0,$strlen)];
    }
    $_SESSION['code'] = $this->code;
    $diff = $this->width/$this->codeNum;
    for ($i=0; $i < $this->codeNum; $i++) { 
      $txtColor = imagecolorallocate($this->img,mt_rand(100,255),mt_rand(100,255),mt_rand(100,255));
      imagettftext($this->img, $this->fontSize, mt_rand(-30,30), $diff*$i+mt_rand(3,8), mt_rand(20,$this->height-10), $txtColor, $this->font, $this->code[$i]);
    }
  }

  public function createLines(){
    for ($i=0; $i < 4; $i++) { 
      $color = imagecolorallocate($this->img,mt_rand(0,155),mt_rand(0,155),mt_rand(0,155));
      imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color); 
    }
  }

  public function createPiexs(){
    for ($i=0; $i < 100; $i++) { 
      $color = imagecolorallocate($this->img,mt_rand(0,255),mt_rand(0,255),mt_rand(0,255));
      imagesetpixel($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
    }
  }

  public function show()
  {
    $this->createImage();
    $this->createCode();
    if ($this->lineFlag) {
      $this->createLines();
    }
    if ($this->piexFlag) {
      $this->createPiexs();
    }
    header('Content-type:image/png');
    imagepng($this->img);
    imagedestroy($this->img);
  }

  public function getCode(){
    return $this->code;
  }
}

以上就是验证码类的全部代码。看起来确实挺简单的,不过用的图像处理函数比较多,上面相关的函数我也做了必要的链接和用途说明。这些函数也不用死记硬背,遇到不清楚的,随时查阅 PHP 官方文档,最重要的是还有中文文档。

2.2 使用验证码

既然已经封装完毕,那就可以开始使用了。这里为了方便,直接在 Captcha 类的下方调用该类:

session_start(); //开启session
$captcha = new Captcha();  //实例化验证码类(可自定义参数)
$captcha->show();  //调用输出

三、前端展示

后端已经准备好了验证码,前端界面就可以展示了,修改 index.php 中的注册与登陆表单的验证码部分:

<div class="form-group">
 <div class="col-sm-12">
   <img src="admin/Captcha.php" alt="" id="codeimg" onclick="javascript:this.src = 'admin/Captcha.php?'+Math.random();">
   <span>Click to Switch</span>
 </div>
</div>

img 标签添加了点击事件的 js 代码,这样就可以实现点击更换验证码的功能!

效果图:

基于PHP实现用户注册登录功能

四、完善

到目前为止,我们的验证码模块基本就完成了。学习到这里,大家应该对面向对象编程有了进一步的理解。也领悟到了一丝 OOP 思想。OOP 的三大特征:封装,继承,多态。我们这里只用到了一点封装的思想。大家可以继续完善和改进这个验证码类,设计出更加完美的类。这个实验也告诉我们,PHP 的函数很多,不要死记硬背,多看官方文档。

PHP 相关文章推荐
dedecms 制作模板中使用的全局标记图文教程
Mar 11 PHP
PHP 函数语法介绍一
Jun 14 PHP
PHP中Date()时间日期函数的使用方法小结
Apr 20 PHP
PHP 第二节 数据类型之转换
Apr 28 PHP
PHP fopen 读取带中文URL地址的一点见解
Sep 25 PHP
深入理解:XML与对象的序列化与反序列化
Jun 08 PHP
php删除数组元素示例分享
Feb 17 PHP
PHP判断远程图片或文件是否存在的实现代码
Feb 20 PHP
php购物车实现方法
Jan 03 PHP
PHP访问数据库集群的方法小结
Mar 14 PHP
php正则表达式基本知识与应用详解【经典教程】
Apr 17 PHP
Laravel中如何增加自定义全局函数详解
May 09 PHP
PHP基于curl后台远程登录正方教务系统的方法
Oct 14 #PHP
php车辆违章查询数据示例
Oct 14 #PHP
基于PHPexecl类生成复杂的报表表头示例
Oct 14 #PHP
php+jQuery递归调用POST循环请求示例
Oct 14 #PHP
php实现将HTML页面转换成word并且保存的方法
Oct 14 #PHP
PHP中多线程的两个实现方法
Oct 14 #PHP
PHP在innodb引擎下快速代建全文搜索功能简明教程【基于xunsearch】
Oct 14 #PHP
You might like
在PHP中利用XML技术构造远程服务(下)
2006/10/09 PHP
[转帖]PHP世纪万年历
2006/12/06 PHP
PHP5中使用PDO连接数据库的方法
2010/08/01 PHP
codeigniter自带数据库类使用方法说明
2014/03/25 PHP
PHP常用技术文之文件操作和目录操作总结
2014/09/27 PHP
php中ftp_chdir与ftp_cdup函数用法
2014/11/18 PHP
PHP新特性详解之命名空间、性状与生成器
2017/07/18 PHP
thinkphp框架表单数组实现图片批量上传功能示例
2020/04/04 PHP
jQuery ajax 路由和过滤器使用说明
2011/08/02 Javascript
js图片实时加载提供网页打开速度
2014/09/11 Javascript
JavaScript定时器和优化的取消定时器方法
2015/07/03 Javascript
无需 Flash 使用 jQuery 复制文字到剪贴板
2016/04/26 Javascript
JS模拟的Map类实现方法
2016/06/17 Javascript
js实现定时进度条完成后切换图片
2017/01/04 Javascript
微信小程序的生命周期的详解
2017/10/19 Javascript
详解通过源码解析Node.js中cluster模块的主要功能实现
2018/05/16 Javascript
javascript实现贪吃蛇小练习
2020/07/05 Javascript
浅谈JavaScript中this的指向更改
2020/07/28 Javascript
JavaScript编写开发动态时钟
2020/07/29 Javascript
ant design vue 表格table 默认勾选几项的操作
2020/10/31 Javascript
Python自动扫雷实现方法
2015/07/25 Python
Python numpy实现数组合并实例(vstack,hstack)
2018/01/09 Python
使用tensorflow根据输入更改tensor shape
2020/06/23 Python
python统计mysql数据量变化并调用接口告警的示例代码
2020/09/21 Python
Python实现哲学家就餐问题实例代码
2020/11/09 Python
HTML5实现可缩放时钟代码
2017/08/28 HTML / CSS
使用HTML5和CSS3制作一个模态框的示例
2018/03/07 HTML / CSS
HTML5通过navigator.mediaDevices.getUserMedia调用手机摄像头问题
2020/04/27 HTML / CSS
详解如何解决H5开发使用wx.hideMenuItems无效果不生效
2021/01/20 HTML / CSS
马来西亚领先的在线礼品店:Giftr
2018/08/23 全球购物
中学自我评价
2014/01/31 职场文书
党员2014两会学习心得体会
2014/03/17 职场文书
小学生安全演讲稿
2014/04/25 职场文书
党干部专题民主生活会对照检查材料思想汇报
2014/10/06 职场文书
上课睡觉检讨书300字
2014/11/18 职场文书
SQL Server使用导出向导功能
2022/04/08 SQL Server