用户
 找回密码
 入住 CI 中国社区
搜索
查看: 11981|回复: 16
收起左侧

[Others] 【求助】CI开发验证码程序

[复制链接]
发表于 2008-3-7 16:50:53 | 显示全部楼层 |阅读模式
初次接触CI,目前在用CI做验证码程序,大体思路还是和以前一样,通过gd生成图片,改成MVC模式不是很明白如何使用,能否有人指导一下。或者给个例程,不甚感激。
发表于 2008-3-9 14:22:13 | 显示全部楼层
我好久没用CI, Captcha库都不记得是否使用过, 所以下面的代码调试不出来,是正常的;-) 但修正应该不是什麽难事.
我不喜欢生成图片然后再删除这样的东西,自己参照别人写了这个,保存到app目录的libraries目录里,这里假设文件名是CaptchaImage.php
PHP复制代码
class CaptchaImage{
 
    var $length;
    var $string;
    var $ttfFont;
    var $charWidth;
 
    //var $background_colour;
    //var $text_colour;
    //var $colours = array();
    //var $shadow_height;
    //var $shadow_dark;
 
 /**
  * PHP4 constructor.
  *
  * @access   public
  * @param    string   $length   captcha string length
  * @param    string   $string   captcha string
  * @return   void
  */

  function __construct($length = 7, $string = null){
 
    $this->length    = $length;
    $this->ttfFont   = './App/fonts/captcha.ttf';
    $this->charWidth = 12;
        $this->font_size = 9;
    if (!isset($string)){
      $this->mystring = $this->randomString($length);
    } else {
      $this->mystring = $string;
    }
 
    //$this->background_colour = 'FFFFFF';
        //$this->text_colour = '000000';
        //$this->colours = array('003366', 'CCD6E0', '7F99B2','F7EFC6', 'C6BE8C', 'CC6600','990000','520000','BFBFC1','808080'); // colors of the slices.
        //$this->shadow_height = 16;
        //$this->shadow_dark = true;
 
  }
 
  function setFont($ttfpath, $ttfsize = 8){
        if(file_exists($ttfpath)){
            $this->ttfFont = $ttfpath;
            $this->font_size = $ttfsize;
        } else {
            $this->ttfFont = 2;
            //echo 'font can\'t be found';
        }
    }
 
  function randomString($length){
 
    //alphanumeric array widthout ambiguous chars
    $alphanumericArr  = array ('A','B','C','D','E','F','G','H','L','M','P','R','2','3','8','9');
    shuffle($alphanumericArr);
    $alphanumericArr = array_slice($alphanumericArr, 0, $length-1);
    $string = join("",$alphanumericArr);
    return $string;
 
  }
 
 
  function draw(){
        header("Cache-Control: no-store, no-cache, must-revalidate"); // HTTP/1.1
        header("Cache-Control: post-check=0, pre-check=0", false);
        header("Pragma: no-cache");
        header("Content-Type: image/png");
        header("Expires: Mon, 26 Jul 1997 05:00:00 GMT"); // Date in the past
        header("Last-Modified: ".gmdate("D, d M Y H:i:s", time())." GMT");
 
    $length     = $this->length * $this->charWidth + 20;
    $height     = 25;
 
    $image      = imagecreate($length, $height);
    $background = imagecolorallocate($image, 00, 0, 0);
 
    $textcolor  = imagecolorallocate($image, 255,255,255);
    $linecolor  = imagecolorallocate($image, 0, 0, 255);
        $border_color    = ImageColorAllocate($image, 153, 102, 102);
 
 
   $this->DrawLines($image, $length, $height);
    imagettftext($image, $this->font_size, 0, 4, 18,
                 $textcolor,
                 $this->ttfFont,
                 $this->mystring);
    // -----------------------------------
    //  Create the border
    // -----------------------------------
    imagerectangle($image, 0, 0, $length-1, $height-1, $border_color);
 
    // -----------------------------------
    //  Generate the image
    // -----------------------------------
    imagepng($image);
 
    //echo $this->ttfFont;
    exit();
 
  }
 
 
  function getString(){
    return $this->mystring;
  }
 
    function _colourHex($img, $HexColorString){
        $R = hexdec(substr($HexColorString, 0, 2));
        $G = hexdec(substr($HexColorString, 2, 2));
        $B = hexdec(substr($HexColorString, 4, 2));
        return ImageColorAllocate($img, $R, $G, $B);
    }
 
    function _colourHexshadow($img, $HexColorString, $mork){
        $R = hexdec(substr($HexColorString, 0, 2));
        $G = hexdec(substr($HexColorString, 2, 2));
        $B = hexdec(substr($HexColorString, 4, 2));
        if($mork){
            ($R > 99) ? $R -= 100 : $R = 0;
            ($G > 99) ? $G -= 100 : $G = 0;
            ($B > 99) ? $B -= 100 : $B = 0;
        } else {
            ($R < 220) ? $R += 35 : $R = 255;
            ($G < 220) ? $G += 35 : $G = 255;
            ($B < 220) ? $B += 35 : $B = 255;
        }
        return ImageColorAllocate($img, $R, $G, $B);
    }
 
  function DrawLines($oImage, $length, $height) {
        for ($i = 0; $i < 48; $i++) {
          // allocate colour
          if (true) {
             $iLineColour = imagecolorallocate($oImage, rand(90, 100), rand(100, 140), rand(120, 130));
          } else {
             $iRandColour = rand(100, 250);
             $iLineColour = imagecolorallocate($oImage, $iRandColour, $iRandColour, $iRandColour);
          }
            //imagecolorallocatealpha($oImage, rand(90, 100), rand(100, 140), rand(120, 130), rand(100, 120));
          // draw line
          imageline($oImage, rand(0, $length), rand(0, 0), rand(0, $length), rand($height, $height), $iLineColour);
        }
    }
 
}
复制代码


控制器
PHP复制代码
<?php if (! defined ('BASEPATH')) die('No direct script access allowed.');
 
class Captcha extends Controller {
 
    function __construct() {
        parent::Controller();
    }
 
    function index() {
       $this->load->view("v_captcha");
    }
    function draw(){
         $this->load->library('captchaimage');
        ob_start();
          //session_start();
        $this->session->set_flashdata('captcha', $this->captchaimage->getString());
        $this->captchaimage->setFont('./App/fonts/captcha.ttf',25); //这个字体自己google一个,下载到你本地目录!!!
        //echo file_exists($ttfpath)
        $this->captchaimage->draw();
          //echo time(); //needed to force image expiration
          //session_write_close();
          ob_flush();
        //    return;
    }
    function valid()
    {
        if($this->input->post("security_code")==$this->session->get("captcha")){
            echo "good, u key the right words";
        }else{
            echo "bad man, try again.";
        }
    }
}
 
?>
复制代码


视图v_captcha.php
HTML复制代码
<form method="post" action="captcha/valid">
<input type="text" name="security_code" maxlength="6"/>
<img src="./draw" title="Security Code" alt="Code" />
<button type="submit"  />
</form>
复制代码
发表于 2008-3-9 17:55:21 | 显示全部楼层
好代码啊!我给加亮一下~
发表于 2008-3-9 21:28:56 | 显示全部楼层
本来语法加亮这些琐事不该再劳烦老大,但每次我都不得要领,怎么加都不亮
发表于 2008-3-9 23:15:43 | 显示全部楼层
哈哈,我的工作就是为大家服务~呵呵
发表于 2008-3-13 10:05:48 | 显示全部楼层
$this->session->get()
Fatal error: Call to undefined method CI_Session::get() in D:\www\my\system\application\controllers\captcha.php on line 25
怎么回事
发表于 2008-3-13 10:21:49 | 显示全部楼层
呵呵,应该是他用的不是 CI 里的 Session 类吧,你可以把这个改一下,我记得是 $this->session->userdata('xxx')    试试吧
发表于 2008-3-13 10:26:53 | 显示全部楼层
我在Session Class 里面也找不到flashdata(),set_flashdata()這些方法
发表于 2008-3-13 10:55:44 | 显示全部楼层
./App/fonts/captcha.ttf
这个目录在CI里面没有啊,fonts目录是在system目录下的,不是在App目录下面吧
发表于 2008-3-13 11:09:33 | 显示全部楼层
flash data 是 1.6.1 里面的新方法,1.6.1 的 Session 类有 flash data

本版积分规则