基于PHP实现用户注册和登录功能
介绍了基于PHP的用户注册和登录功能的实现。本项目分为四个部分:1前端页面制作,2验证码制作,3注册登录,4完善功能。细节可以往下看。
验证码制作
一、实验介绍
这个实验将引导你用面向对象的思想封装一个验证码类。并在注册登录界面显示。通过这个实验的学习,你会了解到PHP的OOP思想,GD库的使用以及验证码的生成。
1.1涉及的知识点
PHP GD库的OOP编程1.2开发工具
威震,一个方便快捷的文本编辑器。点击桌面左下角:应用菜单/开发/升华
第二,封装验证码类
2.1建立目录和准备字体
在网络目录下建立一个管理目录作为我们的后台目录来存储后台代码文件。在admin下设置一个字体目录来存储制作验证码所需的字体。
在admin下创建一个新的Captcha.php文件,这是我们需要编辑的验证码文件。
当前目录层次结构:
编辑Captcha.php文件:
?PHP/* * *验证码类*/验证码类{function _ _ construct () {# code.}}添加该类的私有属性和构造方法:
?php /*** Captcha类*/Captcha类{ private $ codeNum//验证码位数私有$ width//验证码图片宽度私密$高度;//验证码图片高度私密$ img//图像资源句柄private $ lineFlag//是否生成干扰线路私有$ piexFlag//是否生成干扰点私有$ fontSize//字体大小私有$ code//验证码字符private $ string//生成验证码私有$font的字符集;//font function _ _ construct($ codenum=4,$ height=50,$ width=150,$ fontsize=20,$ lineflag=true,$ piex flag=true){ $ this-string=' qwerty upmkjnhbgvfcds xa123456789 ';//删除一些类似的字符$ this-codeNum=$ codeNum;$ this-height=$ height;$ this-width=$ width;$ this-line flag=$ line flag;$ this-piex flag=$ piex flag;$this-font=dirname(__FILE__)。/font/consola . TTF ';$ this-font size=$ font size;}}可以通过以下命令将字体文件下载到字体目录:
$ wget http://labfile.oss.aliyuncs.com/courses/587/consola.ttf
然后开始写具体的方法:
创建图像资源句柄
//Create image资源公共函数Create image(){ $ this-img=image Create($ this-width,$ this-height);//创建图像资源imagecolorclocate($ this-img,mt _ rand (0,100),mt _ rand (0,100),mt _ rand (0,100));//填充图像背景(使用浅色)}使用的相关函数
Imagecreate:基于调色板创建一个新图像imagecolorallocate:为图像分配一种颜色mt_rand:生成一个更好的随机数来创建一个验证码字符串并将其输出到图像
//创建验证码公共函数Create code(){ $ strlen=strlen($ this-string)-1;for($ I=0;$ I $ this-CodeNum;$ I){ $这个-代码。=$this-string[mt_rand(0,$ strlen)];//从字符集中随机取出四个字符拼接} $ _ SESSION[' code ']=$ this-code;//加入会话//计算每个字符之间的间距$ diff=$ this-width/$ this-codenum;for($ I=0;$ I $ this-CodeNum;$i) {//为每个字符生成颜色(使用深色)$ txt color=imagecolor locate($ this-img,mt _ rand (100,255),mt _ rand (100,255));//写image imagettftext ($ this-img,$ this-fontsize,mt _ rand (-30,30),$ diff * $ I mt _ rand (3,8),mt _ rand (20,$ this-height-10),$ txtcolor,$ this。}}使用的相关功能
Imagecreate:基于调色板创建一个新图像imagecolorallocate:为图像分配一种颜色mt_rand:生成一个更好的随机数来创建一个验证码字符串并将其输出到图像
//创建验证码公共函数create code(){ $ strlen=strlen($ this-string)-1;对于($ I=0;$ I $ this-CodeNum;$ I){ $这个-代码=$this-string[mt_rand(0,$ strlen)];//从字符集中随机取出四个字符拼接} $ _ SESSION[' code ']=$ this-code;//加入会议中//计算每个字符间距$ diff=$ this-width/$ this-CodeNum;对于($ 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:用微软和苹果公司共同研制的字型标准字体向图像写入文本创建干扰线条
//创建干扰线条(默认四条)公共函数createLines(){ for($ I=0;$ I 4;$ I){ $ color=image color allocate($ 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:画一条线段创建干扰点
//创建干扰点(默认一百个点)公共函数createPiex(){ for($ I=0;$ I 100 $ I){ $ color=image color allocate($ 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:画一个单一像素对外输出图像:
公共函数show(){ $ this-createImage();$ this-create COde();if ($this-lineFlag) { //是否创建干扰线条$ this-createLines();} if ($this-piexFlag) { //是否创建干扰点$ this-createPiex();}标题(“内容类型:图像/png”);//请求页面的内容是巴布亚新几内亚格式的图像image png($ this-img);//以巴布亚新几内亚格式输出图像图像销毁($ this-img);//清除图像资源,释放内存}用到的相关函数:
imagepng:以巴布亚新几内亚格式将图像输出到浏览器或文件imagedestroy:销毁一图像对外提供验证码:
公共函数GetCode(){ return $ this-code;}完整代码如下:php /*** Captcha类*/Captcha类{ private $ codeNumprivate $ width二等兵$身高;private $ imgprivate $ line flag private $ piexflag private $ font size私有$代码;私人$字符串;私人$字体;function __construct($codeNum=4,$height=50,$width=150,$fontSize=20,$lineFlag=true,$ piexFlag=true){ $ this-string=' qwertyupmkjnhbgvfcds xa123456789 ';$ this-codeNum=$ codeNum;$ this-height=$ height;$ this-width=$ width;$ this-line标志=$ line标志;$ this-piex标志=$ piex标志;$this-font=dirname(__FILE__)./font/consola。' TTF ';$ this-font size=$ font size;} public function create image(){ $ this-img=image create($ this-width,$ this-height);imagecolorable($ this-img,mt_rand(0,100),mt_rand(0,100),mt_rand(0,100));} public function create code(){ $ strlen=strlen($ this-string)-1;对于($ I=0;$ I $ this-CodeNum;$ I){ $这个-代码=$this-string[mt_rand(0,$ strlen)];} $ _ SESSION[' code ']=$ this-code;$ diff=$ this-width/$ this-CodeNum;对于($ I=0;$ I $ this-CodeNum;$ I){ $ TxtColor=image color allocate($ 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]);} }公共函数createLines(){ for($ I=0;$ I 4;$ I){ $ color=image color allocate($ 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=image color allocate($ 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-create COde();if($ this-line标志){ $ this-create line();} if($ this-piex标志){ $ this-createpiex s();}标题(“内容类型:图像/png”);image png($ this-img);图像销毁($ this-img);} public function getCode(){ return $ this-code;}}以上就是验证码类的全部代码。看起来确实挺简单的,不过用的图像处理函数比较多,上面相关的函数我也做了必要的链接和用途说明。这些函数也不用死记硬背,遇到不清楚的,随时查阅服务器端编程语言(专业超文本预处理器的缩写)官方文档,最重要的是还有中文文档。
2.2 使用验证码
既然已经封装完毕,那就可以开始使用了。这里为了方便,直接在验证码类的下方调用该类:
session _ start();//开启session $ Captcha=new Captcha();//实例化验证码类(可自定义参数)$验证码-show();//调用输出三、前端展示
后端已经准备好了验证码,前端界面就可以展示了,修改index.php中的注册与登陆表单的验证码部分:
div class=' form-group ' div class=' col-sm-12 ' img src=' http : admin/Captcha。PHP ' alt=' id=' codeimg ' onclick=' JavaScript : this。src=' admin/Captcha。PHP?数学。random();“跨度单击切换/span/div/div标签添加了点击事件的射流研究…代码,这样就可以实现点击更换验证码的功能!
效果图:
四、完善
到目前为止,我们的验证码模块基本就完成了。学习到这里,大家应该对面向对象编程有了进一步的理解。也领悟到了一丝面向对象的程序设计(面向对象编程)思想面向对象程序。的三大特征:封装,继承,多态。我们这里只用到了一点封装的思想。大家可以继续完善和改进这个验证码类,设计出更加完美的类。这个实验也告诉我们,PHP的函数很多,不要死记硬背,多看官方文档。
版权声明:基于PHP实现用户注册和登录功能是由宝哥软件园云端程序自动收集整理而来。如果本文侵犯了你的权益,请联系本站底部QQ或者邮箱删除。

















