CI验证码修改优化

来源:互联网 发布:宁德catl知乎 编辑:程序博客网 时间:2024/05/24 06:30

   验证码机制在CI框架中是通过一个辅助函数captcha()进行实现的——验证码辅助函数文件包含了一些帮助你创建验证码图片的函数。。

        那么我们如何使用CI的captcha()辅助函数来完成验证码功能呢?下面我会先讲述如何使用CI的captcha()辅助函数来完成验证码功能,然后在讲述如何具体的对CI框架的验证码机制进行优化。

1、CI框架验证码功能的使用

a)  首先我们要先加载辅助函数

加载辅助函数一共有两种方法:

①、自动加载

        我们可以在根文件目录下的 “application/config/autoload.php” 文件中进行设置自动加载。        

[php] view plain copy
  1. //ci框架设置自动加载辅助函数  
  2. //captcha验证码复制函数  
  3. $autoload['helper'] = array('url','captcha');  
        由于我们的项目使用验证码的地方非常有限,故而不推荐使用自动加载这种方法,我们可以在使用到的地方加载使用就可以了。

②、在使用到的地方进行加载

这种方法我们还是比较推荐的,消耗资源较少,效率会稍微的高一点。在你使用到验证码的控制器中写一个构造函数,在构造函数中进行验证码辅助函数加载就可以了。

[php] view plain copy
  1. //构造函数  
  2. public function __construct()  
  3. {  
  4.     //切记在控制器的构造函数中一定先继承父类控制器的构造函数  
  5.     parent::__construct();$this->load->helper('captcha');  
  6. }  

b) 然后使用验证码辅助函数创建验证码

[php] view plain copy
  1. $vals = array(  
  2.             'word'          => 'Random word',        //验证码上显示的字符,可以写成函数,例如:rand(100000,999999)  
  3.                 'img_path'      => './data/captcha/',    //验证码保存路径  
  4.                 'img_url'       => base_url('data/captcha'), //验证码图片url  
  5.             'font_path'     => './path/to/fonts/texb.ttf',   //验证码上字体  
  6.             'img_width'     => '150',        //验证码图片宽度  
  7.                 'img_height'    => 30,           //验证码图片高度  
  8.                 'expiration'    => 7200,         //验证码图片删除时间  
  9.                 'word_length'   => 8,            //验证码长度  
  10.                 'font_size'     => 16,           //验证码字体大小  
  11.                 'img_id'        => 'Imageid',  
  12.                 'pool'          => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',  
  13.                 'colors'        => array(  
  14.                             'background' => array(255, 255, 255),  
  15.                         'border' => array(255, 255, 255),  
  16.                         'text' => array(0, 0, 0),  
  17.                         'grid' => array(255, 40, 40)  
  18.                 ),  
  19.         );  
  20.   
  21.         $cap = create_captcha($vals);  
  22.         var_dump($cap);  
        这样验证码就创建完成,img_path和img_url这俩个参数必须存在,并且,img_path所表示的路径文件夹必须存在,不然的话创建验证码不会成功。由于每创建一次验证码就会生成一张图片放到你设置的文件夹中,这样是非常消耗资源的,故此我们要对CI框架的验证码功能进行优化。

2、CI框架验证码的优化

优化思路:①、我们不让框架生成的图片进行保存到服务器中;②、我们只保留验证码的的内容即可。

要想对验证码功能进行优化,我们就要对验证码辅助函数功能进行扩展。

a)  扩展验证码辅助函数

首先将根目录下 “system/helpers/captcha_helper.php” 文件复制一份到根目录下 "application/helpers" 目录下,命名为 "MY_captcha_helper.php" ;

然后将下面代码注释掉(大概在96行到119行);

[php] view plain copy
  1. if ($img_path === '' OR $img_url === '' OR ! is_dir($img_path) OR ! is_really_writable($img_path) OR ! extension_loaded('gd'))  
  2.         {  
  3.             return FALSE;  
  4.         }  
  5.   
  6.         // -----------------------------------  
  7.         // Remove old images  
  8.         // -----------------------------------  
  9.   
  10.         $now = microtime(TRUE);  
  11.   
  12.         $current_dir = @opendir($img_path);  
  13.         while ($filename = @readdir($current_dir))  
  14.         {  
  15.             if (substr($filename, -4) === '.jpg' && (str_replace('.jpg'''$filename) + $expiration) < $now)  
  16.             {  
  17.                 @unlink($img_path.$filename);  
  18.             }  
  19.         }  
  20.   
  21.         @closedir($current_dir);  
此段代码防止你没有传递img_path和img_url参数以及参数所指的文件夹不存在就暂停执行函数。

再次注释代码(大概在318行到335行)

[php] view plain copy
  1. $img_url = rtrim($img_url'/').'/';  
  2.   
  3.         if (function_exists('imagejpeg'))  
  4.         {  
  5.             $img_filename = $now.'.jpg';  
  6.             imagejpeg($im$img_path.$img_filename);  
  7.         }  
  8.         elseif (function_exists('imagepng'))  
  9.         {  
  10.             $img_filename = $now.'.png';  
  11.             imagepng($im$img_path.$img_filename);  
  12.         }  
  13.         else  
  14.         {  
  15.             return FALSE;  
  16.         }  
  17.   
  18.         $img = '<img '.($img_id === '' ? '' : 'id="'.$img_id.'"').' src="'.$img_url.$img_filename.'" style="width: '.$img_width.'; height: '.$img_height .'; border: 0;" alt=" " />';  
此段代码用于创建验证码图片,并且将图片保存到你说创建的验证码文件夹中(image_path)。

最后,在create_captcha()函数的最后加上一个header头,最后代码如下:

[php] view plain copy
  1. //直接输出  
  2.         header("Content-Type:image/jpeg");      //加入图片格式header头  
  3.         imagejpeg($im);  
  4.         ImageDestroy($im);  
  5.         //返回生成的验证码字符串,如果需要其他参数的话也可以加入返回  
  6.         return $word;  
  7.         //return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);  

b) 应用扩展优化之后的验证码功能

首先在控制器中写一个生成验证码方法;

然后在方法中进行调用验证码辅助函数,生成验证码;

最后在前台进行调用方法,并实现点击刷新功能。

生成验证码函数代码:

[php] view plain copy
  1. //生成验证码  
  2.     public function code()  
  3.     {  
  4.         //调用函数生成验证码,上述的参数也可以继续使用  
  5.         $vals = array(  
  6.             'word_length' => 6,  
  7.         );  
  8.         create_captcha($vals);  
  9.     }  
前台调用饼实时刷新调用:
[html] view plain copy
  1. <td colspan="2" align="right">  
  2.   <img src="<?php echo site_url('admin/privilege/code');?>" alt="" onclickthis.src="<?php echo site_url('admin/privilege/code').'/'?>"+Math.random() style="cursor: pointer;" title="看不清?点击更换另一个验证码。"/>  
  3. </td>  

至此,CI框架的验证码功能机制优化我们就完成了。

原创粉丝点击