2015-06-24 24 views
1

如何获取Codeigniter验证码图像库。配置erquired定义'img_path' => '', 'img_url' => '',,但我没有这样的文件夹或图像,我红洙很多教程和笨文档以及..但没有一个描述如该验证码库位于..如何获取Codeigniter验证码图像库

功能

public function captcha() { 
     /* Set a few basic form validation rules */ 
     $this->form_validation->set_rules('name', "Name", 'required'); 
     $this->form_validation->set_rules('captcha', "Captcha", 'required'); 

     /* Get the user's entered captcha value from the form */ 
     $userCaptcha = set_value('core/captcha'); 

     /* Get the actual captcha value that we stored in the session (see below) */ 
     $word = $this->session->userdata('captchaWord'); 



     /* Check if form (and captcha) passed validation */ 
     if ($this->form_validation->run() == TRUE && 
       strcmp(strtoupper($userCaptcha), strtoupper($word)) == 0) { 
      /** Validation was successful; show the Success view * */ 
      /* Clear the session variable */ 
      $this->session->unset_userdata('captchaWord'); 


      /* Get the user's name from the form */ 
      $name = set_value('name'); 

      /* Pass in the user input to the success view for display */ 
      $data = array('name' => $name); 
      $this->load->view('pages/captcha-success-view', $data); 
     } else { 

      /** Validation was not successful - Generate a captcha * */ 
      /* Setup vals to pass into the create_captcha function */ 
      $vals = array(
       'word' => 'Random word', 
       'img_path' => '', 
       'img_url' => '', 
       'img_width' => '150', 
       'img_height' => 30, 
       'expiration' => 7200 
      ); 

      print_r($vals); 

      /* Generate the captcha */ 
      $captcha = create_captcha($vals); 

      $data['captcha'] = $captcha; 
      $data['image'] = $captcha['image']; 

      /* Store the captcha value (or 'word') in a session to retrieve later */ 
      $this->session->set_userdata('captchaWord', $captcha['word']); 

      /* Load the captcha view containing the form (located under the 'views' folder) */ 
      $this->load->view('pages/captcha-view', $data); 
     } 
    } 
+0

具有负载验证码助手或什么错误你长了? –

+0

它只是验证码助手...没有图像文件夹? – KBK

+0

你不得不在这里提到你的文件夹路径'img_path'=>'', –

回答

3

CodeIgniter中没有验证码库,但有助手。所有你必须做的加载这个帮手首先使用captcha $this->load->helper('captcha');

而且记住,你必须在你的项目中创建一个文件夹,其中CI将把captcha图像。所以,如果你在你的项目根目录创建一个文件夹名称“验证码”,那么你的代码将是(演示)

$this->load->helper('captcha'); 
$vals = array(
'img_path' => 'captcha/', 
'img_url' => base_url('captcha'), 
'font_path' => 'assets/fonts/ALGER.TTF', 
'img_width' => '300', 
'img_height' => 80, 
'font_size' => 24, 
'colors' => array(
    'background' => array(255, 255, 255), 
    'border' => array(0, 0, 0), 
    'text' => array(0, 0, 0), 
    'grid' => array(255, 40, 40) 
) 
); 

$cap = create_captcha($vals); 

现在打印您的验证码,无论你想echo $cap['image']