当前位置:Gxlcms > PHP教程 > 关于CodeIgniter框架验证码类库文件与用法的分析

关于CodeIgniter框架验证码类库文件与用法的分析

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

这篇文章主要介绍了CodeIgniter框架验证码类库文件与用法,结合实例形式分析了CodeIgniter框架验证码类库文件的定义与具体使用方法,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了CodeIgniter框架验证码类库文件与用法。分享给大家供大家参考,具体如下:

折腾了我四五个小时,终于,ci的验证码类库成功的整出来了。

下面请看源码:

在application/libraries建立Authcode.php文件,代码如下:

  1. <?php
  2. class Authcode
  3. {
  4. var $CI;
  5. var $fontPath;//字体路径
  6. var $image;
  7. var $charLen = 4; //生成几位验证码
  8. var $arrChr = array();//验证码字符
  9. var $width = 83; //图片宽
  10. var $height = 24; //图片高
  11. var $bgcolor = "#ffffff"; //背景色
  12. var $showNoisePix = true; //生成杂点
  13. var $noiseNumPix = 80; //生成杂点数量
  14. var $showNoiseLine = true; //生成杂线
  15. var $noiseNumLine = 2; //生成杂线数量
  16. var $showBorder = true; //边框,当杂点、线一起作用的时候,边框容易受干扰
  17. var $borderColor = "#000000";
  18. function Authcode()
  19. {
  20. $this->CI = & get_instance();
  21. $this->fontPath = realpath(dirname(__FILE__) . '/fonts/'); //字体文件
  22. //$this->arrChr = array_merge(range(1, 9) , range('A', 'Z'));//数字字母验证码
  23. //$this->arrChr = range('A', 'Z');//纯字母验证码
  24. $this->arrChr = range(0, 9);//纯数字验证码
  25. }
  26. /**
  27. * 显示验证码
  28. *
  29. */
  30. function show()
  31. {
  32. $this->image = imageCreate($this->width, $this->height);
  33. $this->back = $this->getColor($this->bgcolor);
  34. imageFilledRectangle($this->image, 0, 0, $this->width, $this->height, $this->back);
  35. $size = $this->width / $this->charLen - 4;
  36. if ($size > $this->height) {
  37. $size = $this->height;
  38. }
  39. $left = ($this->width - $this->charLen * ($size + $size / 10)) / $size + 5;
  40. $code = '';
  41. for($i = 0; $i < $this->charLen; $i ++) {
  42. $randKey = rand(0, count($this->arrChr) - 1);
  43. $randText = $this->arrChr[$randKey];
  44. $code .= $randText;
  45. $textColor = imageColorAllocate($this->image, rand(0, 100), rand(0, 100), rand(0, 100));
  46. $font = $this->fontPath . '/' . rand(1, 5) . ".ttf";
  47. $randsize = rand($size - $size / 10, $size + $size / 10);
  48. $location = $left + ($i * $size + $size / 10);
  49. @imagettftext($this->image, $randsize, rand(- 18, 18), $location, rand($size - $size / 10, $size + $size / 10) + 2, $textColor, $font, $randText);
  50. }
  51. if ($this->showNoisePix == true) {
  52. $this->setNoisePix();
  53. }
  54. if ($this->showNoiseLine == true) {
  55. $this->setNoiseLine();
  56. }
  57. if ($this->showBorder == true) {
  58. $this->borderColor = $this->getColor($this->borderColor);
  59. imageRectangle($this->image, 0, 0, $this->width - 1, $this->height - 1, $this->borderColor);
  60. }
  61. $this->CI->session->set_userdata('auth_code', $code);
  62. ob_clean();
  63. header("Content-type: image/jpeg");
  64. imagejpeg($this->image);
  65. imagedestroy($this->image);
  66. }
  67. /**
  68. * 显示验证码的JS调用
  69. *
  70. */
  71. function showScript()
  72. {
  73. //显示验证码
  74. echo "var img_src = '/imgauthcode/show/?';\n";
  75. echo "document.writeln('<img id=\"img_authcode\" src=\"' + img_src + Math.random() + '\" style=\"cursor:hand;\" onclick=\"this.src=img_src + Math.random();\" alt=\"点击更换图片\">');";
  76. }
  77. /**
  78. * 检查验证码是否正确
  79. *
  80. * @param string $auth_code
  81. * @return bool
  82. */
  83. function check($auth_code = null)
  84. {
  85. return ($this->CI->session->userdata('auth_code') && $auth_code) ? ($this->CI->session->userdata('auth_code') === $auth_code) : false;
  86. }
  87. function getColor($color)
  88. {
  89. $color = eregi_replace("^#", "", $color);
  90. $r = $color[0] . $color[1];
  91. $r = hexdec($r);
  92. $b = $color[2] . $color[3];
  93. $b = hexdec($b);
  94. $g = $color[4] . $color[5];
  95. $g = hexdec($g);
  96. $color = imagecolorallocate($this->image, $r, $b, $g);
  97. return $color;
  98. }
  99. function setNoisePix()
  100. {
  101. for($i = 0; $i < $this->noiseNumPix; $i ++) {
  102. $randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
  103. imageSetPixel($this->image, rand(0, $this->width), rand(0, $this->height), $randColor);
  104. }
  105. }
  106. function setNoiseLine()
  107. {
  108. for($i = 0; $i < $this->noiseNumLine; $i ++) {
  109. $randColor = imageColorAllocate($this->image, rand(0, 255), rand(0, 255), rand(0, 255));
  110. imageline($this->image, rand(1, $this->width), rand(1, $this->height), rand(1, $this->width), rand(1, $this->height), $randColor);
  111. }
  112. }
  113. }

Authcode.php代码结束

在Controller中,有个admin类,其中有两个方法:

  1. Class Admin extends CI_Controller{
  2. function __construct()
  3. {
  4. parent::__construct();
  5. $this->load->library('Authcode');
  6. }
  7. function captcha(){
  8. if($_POST){
  9. if ($this->authcode->check($this->input->post('gd_pic'))) {
  10. echo "right";
  11. } else {
  12. echo '验证码不正确,请重新输入';
  13. }
  14. }else{
  15. $this->load->view('demo');
  16. }
  17. }
  18. function show_captcha(){ //此方法用于显示验证码图片,归一个view中的img的src调用
  19. $this->authcode->show();
  20. }
  21. }

下面是在视图view中创建一个demo.php了,代码如下:

  1. <?php echo form_open('c=admin&m=captcha');?>
  2. <input type="text" name="gd_pic" />
  3. <img src="<?php echo base_url('?c=admin&m=show_captcha');?>" ><br>
  4. <input type="submit" name="submit" value="验证" />
  5. <?php echo form_close();?>

OK. 一切结束,终于正常运行了。

以上就是本文的全部内容,希望对大家的学习有所帮助,更多相关内容请关注PHP中文网!

相关推荐:

关于CI框架的表单验证分析

如何解决Nginx和CI框架出现404错误

以上就是关于CodeIgniter框架验证码类库文件与用法的分析的详细内容,更多请关注Gxl网其它相关文章!

人气教程排行