一个漂亮的php验证码类

[复制链接]
查看1557 | 回复0 | 2021-3-11 11:15 | 显示全部楼层 |阅读模式
  1. //验证码类
  2. class ValidateCode {
  3. private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子
  4. private $code;//验证码
  5. private $codelen = 4;//验证码长度
  6. private $width = 130;//宽度
  7. private $height = 50;//高度
  8. private $img;//图形资源句柄
  9. private $font;//指定的字体
  10. private $fontsize = 20;//指定字体大小
  11. private $fontcolor;//指定字体颜色
  12. //构造方法初始化
  13. public function __construct() {
  14.   $this->font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片
  15. }
  16. //生成随机码
  17. private function createCode() {
  18.   $_len = strlen($this->charset)-1;
  19.   for ($i=0;$i<$this->codelen;$i++) {
  20.    $this->code .= $this->charset[mt_rand(0,$_len)];
  21.   }
  22. }
  23. //生成背景
  24. private function createBg() {
  25.   $this->img = imagecreatetruecolor($this->width, $this->height);
  26.   $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));
  27.   imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);
  28. }
  29. //生成文字
  30. private function createFont() {
  31.   $_x = $this->width / $this->codelen;
  32.   for ($i=0;$i<$this->codelen;$i++) {
  33.    $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  34.    imagettftext($this->img,$this->fontsize,mt_rand(-30,30),$_x*$i+mt_rand(1,5),$this->height / 1.4,$this->fontcolor,$this->font,$this->code[$i]);
  35.   }
  36. }
  37. //生成线条、雪花
  38. private function createLine() {
  39.   //线条
  40.   for ($i=0;$i<6;$i++) {
  41.    $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));
  42.    imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);
  43.   }
  44.   //雪花
  45.   for ($i=0;$i<100;$i++) {
  46.    $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));
  47.    imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);
  48.   }
  49. }
  50. //输出
  51. private function outPut() {
  52.   header('Content-type:image/png');
  53.   imagepng($this->img);
  54.   imagedestroy($this->img);
  55. }
  56. //对外生成
  57. public function doimg() {
  58.   $this->createBg();
  59.   $this->createCode();
  60.   $this->createLine();
  61.   $this->createFont();
  62.   $this->outPut();
  63. }
  64. //获取验证码
  65. public function getCode() {
  66.   return strtolower($this->code);
  67. }
  68. }
复制代码
使用方法:
1、先把验证码类保存为一个名为 ValidateCode.class.php 的文件;
2、新建一个名为 captcha.php 的文件进行调用该类;
captcha.php
  1. session_start();
  2. require './ValidateCode.class.php';  //先把类包含进来,实际路径根据实际情况进行修改。$_vc = new ValidateCode();  //实例化一个对象$_vc->doimg();  
  3. $_SESSION['authnum_session'] = $_vc->getCode();//验证码保存到SESSION中
复制代码
3、引用到页面中,代码如下:
  1. <img  title="点击刷新" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
复制代码
4、一个完整的验证页面,代码如下:
  1. <?php
  2. session_start();
  3. //在页首先要开启session,
  4. //error_reporting(2047);
  5. session_destroy();
  6. //将session去掉,以每次都能取新的session值;
  7. //用seesion 效果不错,也很方便
  8. ?>
  9. <html>
  10. <head>
  11. <title>session 图片验证实例</title>
  12. <style type="text/css">
  13. #login p{
  14. margin-top: 15px;
  15. line-height: 20px;
  16. font-size: 14px;
  17. font-weight: bold;
  18. }
  19. #login img{
  20. cursor:pointer;
  21. }
  22. form{
  23. margin-left:20px;
  24. }
  25. </style>
  26. </head>
  27. <body>

  28. <form id="login" action="" method="post">
  29. <p>此例为session验证实例</p>
  30. <p>
  31. <span>验证码:</span>
  32. <input type="text" name="validate" value="" size=10>
  33. <img  title="点击刷新" src="./captcha.php" align="absbottom" onclick="this.src='captcha.php?'+Math.random();"></img>
  34. </p>
  35. <p>
  36. <input type="submit">
  37. </p>
  38. </form>
  39. <?php
  40. //打印上一个session;
  41. //echo "上一个session:<b>".$_SESSION["authnum_session"]."</b><br>";
  42. $validate="";
  43. if(isset($_POST["validate"])){
  44. $validate=$_POST["validate"];
  45. echo "您刚才输入的是:".$_POST["validate"]."<br>状态:";
  46. if($validate!=$_SESSION["authnum_session"]){
  47. //判断session值与用户输入的验证码是否一致;
  48. echo "<font color=red>输入有误</font>";
  49. }else{
  50. echo "<font color=green>通过验证</font>";
  51. }
  52. }
  53. ?>
复制代码


您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

UID
434
贡献
3
丢币
0
主题
59
回帖
0
注册时间
2021-2-21
最后登录
2021-12-28