侯体宗的博客
  • 首页
  • 人生(杂谈)
  • 技术
  • 关于我
  • 更多分类
    • 文件下载
    • 文字修仙
    • 中国象棋ai
    • 群聊
    • 九宫格抽奖
    • 拼图
    • 消消乐
    • 相册

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

php  /  管理员 发布于 8年前   205

本文介绍的是基于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 文件,这就是我们需要编辑的验证码类文件。

当前目录层次结构:

编辑 Captcha.php 文件:

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

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;  }完整代码如下: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 中的注册与登陆表单的验证码部分:

Click to Switch

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

效果图:


四、完善

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

您可能感兴趣的文章:

  • php+redis实现注册、删除、编辑、分页、登录、关注等功能示例
  • Thinkphp框架 表单自动验证登录注册 ajax自动验证登录注册
  • php+mysql实现简单登录注册修改密码网页
  • PHP实现的登录,注册及密码修改功能分析
  • php注册和登录界面的实现案例(推荐)
  • thinkphp框架下实现登录、注册、找回密码功能
  • php注册登录系统简化版
  • ThinkPHP之用户注册登录留言完整实例
  • php自动注册登录验证机制实现代码
  • PHP MYSQL实现登陆和模糊查询两大功能
  • PHP实现的注册,登录及查询用户资料功能API接口示例


  • 上一条:
    php生成与读取excel文件
    下一条:
    PHP基于curl后台远程登录正方教务系统的方法
  • 昵称:

    邮箱:

    0条评论 (评论内容有缓存机制,请悉知!)
    最新最热
    • 分类目录
    • 人生(杂谈)
    • 技术
    • linux
    • Java
    • php
    • 框架(架构)
    • 前端
    • ThinkPHP
    • 数据库
    • 微信(小程序)
    • Laravel
    • Redis
    • Docker
    • Go
    • swoole
    • Windows
    • Python
    • 苹果(mac/ios)
    • 相关文章
    • Laravel从Accel获得5700万美元A轮融资(0个评论)
    • PHP 8.4 Alpha 1现已发布!(0个评论)
    • 用Time Warden监控PHP中的代码处理时间(0个评论)
    • 在PHP中使用array_pop + yield实现读取超大型目录功能示例(0个评论)
    • Property Hooks RFC在PHP 8.4中越来越接近现实(0个评论)
    • 近期文章
    • 在go语言中实现字符串可逆性压缩及解压缩功能(0个评论)
    • 使用go + gin + jwt + qrcode实现网站生成登录二维码在app中扫码登录功能(0个评论)
    • 在windows10中升级go版本至1.24后LiteIDE的Ctrl+左击无法跳转问题解决方案(0个评论)
    • 智能合约Solidity学习CryptoZombie第四课:僵尸作战系统(0个评论)
    • 智能合约Solidity学习CryptoZombie第三课:组建僵尸军队(高级Solidity理论)(0个评论)
    • 智能合约Solidity学习CryptoZombie第二课:让你的僵尸猎食(0个评论)
    • 智能合约Solidity学习CryptoZombie第一课:生成一只你的僵尸(0个评论)
    • 在go中实现一个常用的先进先出的缓存淘汰算法示例代码(0个评论)
    • 在go+gin中使用"github.com/skip2/go-qrcode"实现url转二维码功能(0个评论)
    • 在go语言中使用api.geonames.org接口实现根据国际邮政编码获取地址信息功能(1个评论)
    • 近期评论
    • 122 在

      学历:一种延缓就业设计,生活需求下的权衡之选中评论 工作几年后,报名考研了,到现在还没认真学习备考,迷茫中。作为一名北漂互联网打工人..
    • 123 在

      Clash for Windows作者删库跑路了,github已404中评论 按理说只要你在国内,所有的流量进出都在监控范围内,不管你怎么隐藏也没用,想搞你分..
    • 原梓番博客 在

      在Laravel框架中使用模型Model分表最简单的方法中评论 好久好久都没看友情链接申请了,今天刚看,已经添加。..
    • 博主 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 @1111老铁这个不行了,可以看看近期评论的其他文章..
    • 1111 在

      佛跳墙vpn软件不会用?上不了网?佛跳墙vpn常见问题以及解决办法中评论 网站不能打开,博主百忙中能否发个APP下载链接,佛跳墙或极光..
    • 2016-10
    • 2016-11
    • 2017-06
    • 2017-07
    • 2017-08
    • 2017-09
    • 2017-11
    • 2017-12
    • 2018-01
    • 2018-02
    • 2018-03
    • 2020-03
    • 2020-04
    • 2020-05
    • 2020-06
    • 2020-07
    • 2020-09
    • 2021-02
    • 2021-03
    • 2021-04
    • 2021-05
    • 2021-06
    • 2021-07
    • 2021-08
    • 2021-09
    • 2021-10
    • 2021-11
    • 2021-12
    • 2022-01
    • 2022-02
    • 2022-05
    • 2022-06
    • 2022-07
    • 2022-08
    • 2022-09
    • 2022-10
    • 2022-11
    • 2022-12
    • 2023-01
    • 2023-02
    • 2023-03
    • 2023-04
    • 2023-05
    • 2023-06
    • 2023-07
    • 2023-08
    • 2023-09
    • 2023-10
    • 2023-11
    • 2023-12
    • 2024-01
    • 2024-02
    • 2024-03
    • 2024-04
    • 2024-05
    • 2024-06
    • 2024-07
    • 2024-09
    Top

    Copyright·© 2019 侯体宗版权所有· 粤ICP备20027696号 PHP交流群

    侯体宗的博客