您的位置:首页 > 编程学习 > > 正文

ci框架怎样修改为https协议(CI框架教程之优化验证码机制详解验证码辅助函数)

更多 时间:2022-01-27 01:24:55 类别:编程学习 浏览量:2086

ci框架怎样修改为https协议

CI框架教程之优化验证码机制详解验证码辅助函数

本文实例讲述了CI框架教程之优化验证码机制。分享给大家供大家参考,具体如下:

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

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

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

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

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

①、自动加载

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

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

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

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

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • //构造函数
  • public function __construct()
  • {
  •   //切记在控制器的构造函数中一定先继承父类控制器的构造函数
  •   parent::__construct();$this->load->helper('captcha');
  • }
  • b) 然后使用验证码辅助函数创建验证码

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • $vals = array(
  •     'word'     => 'Random word',    //验证码上显示的字符,可以写成函数,例如:rand(100000,999999)
  •       'img_path'   => './data/captcha/'//验证码保存路径
  •       'img_url'    => base_url('data/captcha'),    //验证码图片url
  •       'font_path'   => './path/to/fonts/texb.ttf',    //验证码上字体
  •       'img_width'   => '150',    //验证码图片宽度
  •       'img_height'  => 30,      //验证码图片高度
  •       'expiration'  => 7200,     //验证码图片删除时间
  •       'word_length'  => 8,      //验证码长度
  •       'font_size'   => 16,      //验证码字体大小
  •       'img_id'    => 'Imageid',
  •       'pool'     => '0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ',
  •       'colors'    => array(
  •               'background' => array(255, 255, 255),
  •             'border' => array(255, 255, 255),
  •             'text' => array(0, 0, 0),
  •             'grid' => array(255, 40, 40)
  •       ),
  • );
  •  
  • $cap = create_captcha($vals);
  • 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行);

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

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

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

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

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • //直接输出
  • header("Content-Type:image/jpeg");   //加入图片格式header头
  • imagejpeg($im);
  • ImageDestroy($im);
  • //返回生成的验证码字符串,如果需要其他参数的话也可以加入返回
  • return $word;
  • //return array('word' => $word, 'time' => $now, 'image' => $img, 'filename' => $img_filename);
  • b) 应用扩展优化之后的验证码功能

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

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

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

    生成验证码函数代码:

  • ?
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • //生成验证码
  • public function code()
  • {
  •     //调用函数生成验证码,上述的参数也可以继续使用
  •     $vals = array(
  •         'word_length' => 6,
  •     );
  •     create_captcha($vals);
  • }
  • 前台调用饼实时刷新调用:

  • ?
  • 1
  • 2
  • 3
  • <td colspan="2" align="right">
  •   <img src="<?php echo site_url('admin/privilege/code');?>" alt="" onclick= this.src="<?php echo site_url('admin/privilege/code').'/'?>"+Math.random() style="cursor: pointer;" title="看不清?点击更换另一个验证码。"/>
  • </td>
  • 至此,CI框架的验证码功能机制优化我们就完成了。

    希望本文所述对大家基于CodeIgniter框架的PHP程序设计有所帮助。

    原文链接:https://blog.csdn.net/Zhihua_W/article/details/52524039

    您可能感兴趣