Chinaunix首页 | 论坛 | 博客
  • 博客访问: 449677
  • 博文数量: 141
  • 博客积分: 211
  • 博客等级: 入伍新兵
  • 技术积分: 1049
  • 用 户 组: 普通用户
  • 注册时间: 2010-09-17 16:25
个人简介

如此经年,望尽千帆。

文章分类

全部博文(141)

文章存档

2014年(73)

2013年(65)

2012年(3)

我的朋友

分类: PHP

2014-09-15 06:32:25

分享一个漂亮的类,一起学习探讨。
验证码核心 类代码:

  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'; //先把类包含进来,实际路径根据实际情况进行修改。
  3. $_vc = new ValidateCode(); //实例化一个对象
  4. $_vc->doimg();
  5. $_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:".$_SESSION["authnum_session"]."
    ";

  42. $validate="";
  43. if(isset($_POST["validate"])){
  44. $validate=$_POST["validate"];
  45. echo "您刚才输入的是:".$_POST["validate"]."
    状态:"
    ;
  46. if($$_SESSION["authnum_session"]){
  47. //判断session值与用户输入的验证码是否一致;
  48. echo "输入有误";
  49. }else{ //
  50. echo "通过验证";
  51. }
  52. }
  53. ?>
阅读(1486) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~