当前位置:Gxlcms > PHP教程 > php实现验证码小程序的方法

php实现验证码小程序的方法

时间:2021-07-01 10:21:17 帮助过:5人阅读

本文主要介绍了基于php实现的验证码小程序的具体实现方法,并做了详细注释,有利于理解与学习,需要的朋友一起来看下吧

验证码功能(个人理解):

  • 减轻服务器的压力(如12306的验证码功能);

  • 防止暴力注册

个人思路:在a-z,A-Z,1-9生成n位随机的数来构成新的验证码。

关于生成验证码的几个小函数

range() //指定范围输出一个数组
a) 如: range(1,9)
array_merge()//合并数组
a) array_merge(数组1,数组2….)
array_rand(数组,数量)
a) 随机从数组中取出几个下标返回一个数组

  • shuffle(数组)//将再一次打乱数组中元素

  • mt_rand(指定一个范围) //生成一个更好的随机数

  • 如: mt_rand(1,5) //生成一个在1-5之间的任意数

生成验证码代码


  1. <?php
  2. $arr1=range('a', 'z');//指定范围
输出一个数组 $arr2=range('A', 'Z'); $arr3=range(1,9); $arr=array_merge($arr1,$arr2,$arr3); //合并数组 $index = array_rand($arr,5); //在$arr中随机取5个数,返回值是$arr的下标 Shuffle($index); $code = '';//定义一个空的字符串来存储生成的验证码用'点'来进行拼接 foreach ($index as $key => $value) {//遍历数组 $code.= $arr[$value];//根据下标取数组中的值 } var_dump($code); ?>


运行结果截图

完善:要把验证码添加到图像中这样的验证码才逼真

在完善之前先介绍有关图像创建的大致步骤

创建图像

方法一: 创建一个真彩色图像 (空画布)

imagecreatetruecolor(width, height) //创建一个真彩色图像

说明:

  • width : 画布的宽度(像素)

  • height: 画布的高度(像素)

  • 返回值为图像资源

注意:

为真彩色图像: 填充颜色

imagefill(image, x, y, color) //为图像资源填充颜色

说明:

  • image //图像资源

  • x,y,填充的坐标点(注意:填充的与此点最接近的颜色)

  • color; //用什么颜色来填充

为真彩色图像: 分配颜色

imagecolorallocate(image, red, green, blue)

说明:

  • image //图像资源

  • red: //红颜色(0-255) 或 0x(00-ff) //即十六进制来表示 (0xff就是255)

  • green//绿颜色(0-255)

  • blue //蓝颜色(0-255)

imagefill和imagecolorallocate的代码演示

在没有给画布填充颜色时的效果

给画布填充颜色时的效果和代码


  1. <?php
  2. //创建图像资源(空白画布)默认显示为黑色
  3. $image = imagecreatetruecolor(300, 400);
  4. //1.image //图像资源
  5. //2.red: //红颜色(0-255) 或 0x(00-ff) //即十六进制来表示 (0xff就是255)
  6. //3.green//绿颜色(0-255)
  7. //4.blue //蓝颜色(0-255)
  8. $color = imagecolorallocate($image, 255, 0, 0);
  9. //1.image //图像资源
  10. //2.x,y,填充的坐标点(注意:填充的与此点最接近的颜色)
  11. //3.color; //用什么颜色来填充
  12. imagefill($image, 0, 0, $color);
  13. //
输出图像 header('content-type:image/jpeg'); imagejpeg($image); //销毁图像资源 imagedestroy($image); ?>


结果截图;

输出图像(以jpeg为例)

输出图像到浏览器

a) header('content-type:image/jpeg'); //设置将图像通过浏览来查看

b) imagejpeg(图像资源)

按文件进行输出图像

a) imagejpeg(图像资源,'图像路径',图像的质量) //质量取值0-100

b) 注意:

注意:只能jpeg格式才有质量这个参数.

销毁图像

imagedestroy($image); //销毁图像,释放内存资源.

注意: 当前生成几个图像资源,就销毁几个.

验证码的整个代码:


  1. <?php
  2. //实例:让文本居于图像的正中
  3. //创建图像资源(空白的画布)
  4. $image = imagecreatetruecolor(100, 50);
  5. $color = imagecolorallocate($image, mt_rand(20,200), mt_rand(20,200), mt_rand(20,200));
  6. //为图像资源填充颜色
  7. imagefill($image, 0, 0, $color);
  8. //绘制图像
  9. $font = 5;
  10. //验证码的开始
  11. $arr1 = range('a','z');
  12. $arr3 = range('A','Z');
  13. $arr2 = range(1,9);
  14. //array_merge — 合并一个或多个数组
  15. $arr = array_merge($arr1,$arr2,$arr3);
  16. $index = array_rand($arr,5); //随机从原数组中找出5个下标
  17. $string = '';
  18. foreach ($index as $value) { //$value 两个功能,即是$index中的值,又是$arr中的下标
  19. $string .= $arr[$value]; //将得到字符进行连接
  20. }
  21. //验证码的结束
  22. //mt_rand — 生成更好的随机数
  23. //echo mt_rand(1,5);die;
  24. //加入点干扰
  25. $pointcolor = imagecolorallocate($image, mt_rand(20,200), mt_rand(20,200), mt_rand(20,200));
  26. //循环创建1000个干扰点
  27. for ($i=0; $i <1000 ; $i++) {
  28. imagesetpixel($image, mt_rand(0,imagesx($image)), mt_rand(0,imagesy($image)), $pointcolor);
  29. }
  30. //加入线的干扰
  31. $lintecolor = imagecolorallocate($image, mt_rand(20,200), mt_rand(20,200), mt_rand(20,200));
  32. // 循环创建50个线干扰
  33. for ($i=0; $i <50 ; $i++) {
  34. imageline($image, mt_rand(0,imagesx($image)), mt_rand(0,imagesy($image)), mt_rand(0,imagesx($image)), mt_rand(0,imagesy($image)) ,$lintecolor);
  35. }
  36. //一个字符的宽度 imagefontwidth($font)
  37. //字符串的个数: strlen(字符串)
  38. //一个字符的宽度*字符串的个数
  39. //所有字符串宽度和= 一个字符的宽度*字符串的个数
  40. //$x = (画布的宽度-所有字符串宽度和)/2
  41. $x = (imagesx($image)-imagefontwidth($font)*strlen($string))/2;
  42. //$y = (画布的高度-字符的高度)/2;
  43. //字符的高度: imagefontheight($font)
  44. $y = (imagesy($image)-imagefontheight($font))/2;
  45. $stringcolor = imagecolorallocate($image, mt_rand(20,200), mt_rand(20,200), mt_rand(20,200));
  46. imagestring($image, $font, $x, $y, $string, $stringcolor);
  47. //
输出图像 header('content-type:image/jpeg'); //设置将图像通过浏览来查看 imagejpeg($image,'',100); //将图像资源输出 //销毁图像资源 imagedestroy($image); //销毁图像


理解代码中的一些函数

加入干扰的点

imagesetpixel(image, x, y, color)

说明:x,y 一个点的坐标

加入干扰的线

imageline(image, x1, y1, x2, y2, color)

说明: x1,y1是线的一个端点坐标; x2,y2是线的另一个端口的坐标; 由两点画一条线

让验证码居于图像的正中


  1. imagefontheight(font)获取字体的高度:
  2. imagefontwidth(font)获取字体的宽度:
  3. strlen(字符串)//获取字符串的长度
  4. imagesx(image) //获取画布的宽度
  5. imagesy(image) //获取画布的高度


最后运行结果

再次完善(和html代码结合起来)

Html代码


  1. <!DOCTYPE html>
  2. <html lang="en">
  3. <head>
  4. <meta charset="UTF-8">
  5. <title>Document</title>
  6. </head>
  7. <body>
  8. <form name='frm' method='post' action=''>
  9. <table width="30%" border="2" align="center" rules="all" cellpadding="10">
  10. <tr>
  11. <th colspan="2">请输入信息</th>
  12. </tr>
  13. <tr>
  14. <th>姓名:</th>
  15. <th><input type="text" name="username"></input></th>
  16. </tr>
  17. <tr>
  18. <th>密码:</th>
  19. <th><input type="password" name="userpwd"></input></th>
  20. </tr>
  21. <tr> 555556
  22. <th>验证码</th>
  23. <th><input type = 'text' name = 'checkcode'></input><img src="21.php" style="cursor:pointer" onclick="this.src='21.php'?+Math.random()"></th>
  24. </tr>
  25. <tr>
  26. <th colspan="2"><input type="submit" name="submit" value="提交"></input></th>
  27. </tr>
  28. </table>
  29. </form>
  30. </body>
  31. </html>


理解;

最后结果截图


以上就是本文的全部内容,希望对大家的学习有所帮助。


相关推荐:

php 验证码程序

php 验证码程序_PHP教程

PHP验证码图片生成程序


以上就是php实现验证码小程序的方法的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行