65.9K
CodeProject 正在变化。 阅读更多。
Home

CodeIgniter 中的验证码插件

starIconstarIconstarIconstarIcon
emptyStarIcon
starIcon

4.92/5 (11投票s)

2008年7月29日

CPOL

2分钟阅读

viewsIcon

98355

downloadIcon

2348

如何在 CodeIgniter 中使用验证码插件。

引言

CAPTCHA - Completely Automated Public Turning test to tell Computers and Humans Apart(**全自动公共图灵测试以区分计算机和人类**)。它用于网页,以防止来自站点外部的自动发布。CodeIgniter 框架在 plugins 文件夹中有一个名为 "captcha_pi.php" 的插件。在这里,我演示了一个如何在您的网站内置 CodeIgniter 框架中使用此验证码插件的示例。

开始吧

创建一个名为 "captcha.php" 的控制器,并将其放置在 "controllers" 文件夹中。该控制器如下所示

<?php
class Captcha extends Controller
{
  public function __construct()
  {
    parent::Controller();

    $this->load->model('captcha_model');

    session_start();
  }

  public function index()
  {
    if(empty($_POST))
    {
      $captcha = $this->captcha_model->generateCaptcha();

      $_SESSION['captchaWord'] = $captcha['word'];

      $data['captcha'] = $captcha;

      $this->load->view('show_view', $data);
    }
    else
    {
      //check captcha matches
      if(strcasecmp($_SESSION['captchaWord'],
                    $_POST['confirmCaptcha']) == 0)
      {
        $this->load->view('success_view');
      }
      else
      {
        $this->load->view('failure_view');
      }
    }
  }
}
?>

这里,我创建了一个名为 "captcha_model" 的模型,它有一个方法 generateCaptcha(),该方法使用 CodeIgniter 验证码插件生成验证码并返回一个数组。验证码单词保存在会话中,提交表单后,会将此会话单词与发布单词进行比较以进行匹配。

如果匹配,即在条件 (strcasecmp($_SESSION['captchaWord'], $_POST['confirmCaptcha']) == 0) 中,则显示成功。否则,显示失败。在这里,我使用了 PHP 的 strcasecmp() 函数来不区分大小写地比较单词。如果您希望用户按所示方式输入验证码单词(即,大写字母用大写,小写字母用小写),则可以使用 strcmp() 函数。

captcha_model.php 如下所示

<?php
class Captcha_model extends Model
{
  private $vals = array();

  private $baseUrl  = 'https:///captchademo/';
  private $basePath = 'D:/apache2triad/htdocs/captchademo/';

  private $captchaImagePath = 'tmp/captcha/';
  private $captchaImageUrl  = 'tmp/captcha/';
  private $captchaFontPath  = 'c:/windows/fonts/verdana.ttf';

  public function __construct($configVal = array())
  {
    parent::Model();

    $this->load->plugin('captcha');

    if(!empty($config))
      $this->initialize($configVal);
    else
      $this->vals = array(
                 'word'          => '',
                 'word_length'   => 6,
                 'img_path'      => $this->basePath
                                 .  $this->captchaImagePath,
                 'img_url'       => $this->baseUrl
                                 .  $this->captchaImageUrl,
                 'font_path'     => $this->captchaFontPath,
                 'img_width'     => '150',
                 'img_height'    => 50,
                 'expiration'    => 3600
               );
  }

  /**
   * initializes the variables
   *
   * @author    Mohammad Jahedur Rahman
   * @access    public
   * @param     array     config
   */
  public function initialize ($configVal = array())
  {
    $this->vals = $configVal;
  } //end function initialize

  //---------------------------------------------------------------

  /**
   * generate the captcha
   *
   * @author     Mohammad Jahedur Rahman
   * @access     public
   * @return     array
   */
  public function generateCaptcha ()
  {
    $cap = create_captcha($this->vals);

    return $cap;
  } //end function generateCaptcha
}
?> 

您需要更改 $baseUrl$basePath$captchaImagePath$captchaImageUrl$captchaFontPath 等。为了存储验证码图像,我首先在 index.php 存在的根目录中创建了一个名为 "tmp" 的文件夹和一个子文件夹 "captcha"。如果您在服务器上运行代码,请确保这两个文件夹具有写入权限。

默认情况下,验证码插件生成 8 个字母的单词。但是,我希望改变这个长度。因此,我对这个插件进行了一些小的修改。在 "captcha_pi.php" 的第 156

$defaults = array('word'       => '',    'img_path' => '',
                  'img_url'    => '',    'img_width' => '150',
                  'img_height' => '30',  'font_path' => '',
                  'expiration' => 7200);  

更改为

$defaults = array('word'      => '',    'word_length' => 8,
                  'img_path'  => '',    'img_url'     => '',
                  'img_width' => '150', 'img_height'  => '30',
                  'font_path' => '',    'expiration'  => 7200);

接下来,第 226

for ($i = 0; $i < 8; $i++)

更改为

for ($i = 0; $i < $word_length; $i++)

现在,如果您将单词长度作为参数传递,它将生成一个您指定的单词长度的验证码图像。

captchademo

关注点

虽然在 CodeIgniter 中使用验证码插件更容易,但我建议您使用 ReCaptcha

您可以在 这里 阅读我的博客。

© . All rights reserved.