php验证码类(漂亮、实用)

分享一个漂亮的php验证码类。

  1. <?php  
  2. //验证码类  
  3. class ValidateCode {  
  4.  private $charset = 'abcdefghkmnprstuvwxyzABCDEFGHKMNPRSTUVWXYZ23456789';//随机因子  
  5.  private $code;//验证码  
  6.  private $codelen = 4;//验证码长度  
  7.  private $width = 130;//宽度  
  8.  private $height = 50;//高度  
  9.  private $img;//图形资源句柄  
  10.  private $font;//指定的字体  
  11.  private $fontsize = 20;//指定字体大小  
  12.  private $fontcolor;//指定字体颜色  
  13.  //构造方法初始化  
  14.  public function __construct() {  
  15.   $this->font = dirname(__FILE__).'/font/elephant.ttf';//注意字体路径要写对,否则显示不了图片  
  16.  }  
  17.  //生成随机码  
  18.  private function createCode() {  
  19.   $_len = strlen($this->charset)-1;  
  20.   for ($i=0;$i<$this->codelen;$i++) {  
  21.    $this->code .= $this->charset[mt_rand(0,$_len)];  
  22.   }  
  23.  }  
  24.  //生成背景  
  25.  private function createBg() {  
  26.   $this->img = imagecreatetruecolor($this->width, $this->height);  
  27.   $color = imagecolorallocate($this->img, mt_rand(157,255), mt_rand(157,255), mt_rand(157,255));  
  28.   imagefilledrectangle($this->img,0,$this->height,$this->width,0,$color);  
  29.  }  
  30.  //生成文字  
  31.  private function createFont() {  
  32.   $_x = $this->width / $this->codelen;  
  33.   for ($i=0;$i<$this->codelen;$i++) {  
  34.    $this->fontcolor = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));  
  35.    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]);  
  36.   }  
  37.  }  
  38.  //生成线条、雪花  
  39.  private function createLine() {  
  40.   //线条  
  41.   for ($i=0;$i<6;$i++) {  
  42.    $color = imagecolorallocate($this->img,mt_rand(0,156),mt_rand(0,156),mt_rand(0,156));  
  43.    imageline($this->img,mt_rand(0,$this->width),mt_rand(0,$this->height),mt_rand(0,$this->width),mt_rand(0,$this->height),$color);  
  44.   }  
  45.   //雪花  
  46.   for ($i=0;$i<100;$i++) {  
  47.    $color = imagecolorallocate($this->img,mt_rand(200,255),mt_rand(200,255),mt_rand(200,255));  
  48.    imagestring($this->img,mt_rand(1,5),mt_rand(0,$this->width),mt_rand(0,$this->height),'*',$color);  
  49.   }  
  50.  }  
  51.  //输出  
  52.  private function outPut() {  
  53.   header('Content-type:image/png');  
  54.   imagepng($this->img);  
  55.   imagedestroy($this->img);  
  56.  }  
  57.  //对外生成  
  58.  public function doimg() {  
  59.   $this->createBg();  
  60.   $this->createCode();  
  61.   $this->createLine();  
  62.   $this->createFont();  
  63.   $this->outPut();  
  64.  }  
  65.  //获取验证码  
  66.  public function getCode() {  
  67.   return strtolower($this->code);  
  68.  }  
  69. }  
  70. ?>  

输出实例:
使用方法:
1、先把验证码类保存为一个名为 ValidateCode.class.php 的文件;
2、新建一个名为 captcha.php 的文件进行调用该类;
captcha.php  
  1. <?php  
  2. session_start();  
  3. require './ValidateCode.class.php';  //先把类包含进来,实际路径根据实际情况进行修改。  
  4. $_vc = new ValidateCode();  //实例化一个对象  
  5. $_vc->doimg();    
  6. $_SESSION['authnum_session'] = $_vc->getCode();//验证码保存到SESSION中  
  7. ?>  

3、引用到页面中,代码如下:  
  1. <img  title="点击刷新" src="./captcha.php" align="absbottom" οnclick="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. //by www.jbxue.com  
  9. ?>  
  10. <html>  
  11. <head>  
  12. <title>session 图片验证实例</title>  
  13. <style type="text/css">  
  14. #login p{  
  15. margin-top: 15px;  
  16. line-height: 20px;  
  17. font-size: 14px;  
  18. font-weight: bold;  
  19. }  
  20. #login img{  
  21. cursor:pointer;  
  22. }  
  23. form{  
  24. margin-left:20px;  
  25. }  
  26. </style>  
  27. </head>  
  28. <body>  
  29.   
  30.   
  31. <form id="login" action="" method="post">  
  32. <p>此例为session验证实例</p>  
  33. <p>  
  34. <span>验证码:</span>  
  35. <input type="text" name="validate" value="" size=10>  
  36. <img  title="点击刷新" src="./captcha.php" align="absbottom" οnclick="this.src='captcha.php?'+Math.random();"></img>  
  37. </p>  
  38. <p>  
  39. <input type="submit">  
  40. </p>  
  41. </form>  
  42. <?php  
  43. //打印上一个session;  
  44. //echo "上一个session:<b>".$_SESSION["authnum_session"]."</b><br>";  
  45. $validate="";  
  46. if(isset($_POST["validate"])){  
  47. $validate=$_POST["validate"];  
  48. echo "您刚才输入的是:".$_POST["validate"]."<br>状态:";  
  49. if($validate!=$_SESSION["authnum_session"]){  
  50. //判断session值与用户输入的验证码是否一致;  
  51. echo "<font color=red>输入有误</font>";  
  52. }else{  
  53. echo "<font color=green>通过验证</font>";  
  54. }  
  55. }  
  56. ?>  

更多php 验证码的内容,请参考:

php 验证码 实例分享
php 验证码 封装类
php自定义大小验证码
php生成扭曲及旋转的验证码图片的实例代码
php仿QQ验证码的实现代码
php 图片验证码
php 彩色验证码
php随机验证码 php生成随机验证码(图文)

php 带有雪花背景的验证码



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值