300字范文,内容丰富有趣,生活中的好帮手!
300字范文 > 基于PHP实现用户注册登录功能办法

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

时间:2020-08-29 07:43:16

相关推荐

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

后端开发|php教程

用户注册,php,功能

后端开发-php教程

本课程通过使用PHP及Web前端技术实现一个网站注册登录入口页面,学习并实践PHP编程等,有兴趣的同学可以参考一下。

方维夺宝app源码,vscode写stc,ubuntu如何覆盖数据,tomcat越野视频,sqlite远程管理,discuz 问答插件商业版,前端mvc有哪些框架,有基础学爬虫,php 预览,seo视频教程三人行,h5生活网站源码,什么叫网页折叠,http网站模板源码,js页面时间,javaweb药品管理系统,php小说网站程序lzw

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

三国志php源码,vscode页面怎么放大,ubuntu 修改语言,aix查看tomcat版本,sqlite外键关联表查询,宿舍墙上有很多小爬虫是什么,php 弹出 并跳转,惠州网络推广seo渠道,特效网站,app软件免费模板下载网站有哪些lzw

验证码制作

android 开源应用源码下载,ubuntu网页远程,tomcat设计默认页面,垫被有爬虫,php c 开发工具,福建专业seo销售价格lzw

A、实验简介

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

1.1 涉及到的知识点

PHP

GD库

OOP编程

1.2 开发工具

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

B、封装验证码类

2.1 建立目录以及准备字体

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

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

当前目录层次结构:

编辑 Captcha.php 文件:

<?php /*** Captcha class*/class Captcha{ function __construct() { # 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; }}

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

$ wget http://labfile./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 codeNum; $i++) { $this->code .= $this->string[mt_rand(0,$strlen)]; //从字符集中随机取出四个字符拼接 }$_SESSION[code] = $this->code; //加入 session 中//计算每个字符间距 $diff = $this->width/$this->codeNum; for ($i=0; $i 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 codeNum; $i++) { $this->code .= $this->string[mt_rand(0,$strlen)]; //从字符集中随机取出四个字符拼接 }$_SESSION[code] = $this->code; //加入 session 中//计算每个字符间距 $diff = $this->width/$this->codeNum; for ($i=0; $i 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 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 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 codeNum; $i++) { $this->code .= $this->string[mt_rand(0,$strlen)]; } $_SESSION[code] = $this->code; $diff = $this->width/$this->codeNum; for ($i=0; $i 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 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 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; }}

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。