创建验证码时出现 Codeigniter 验证码助手错误

Codeigniter captcha helper error when captcha creation

我从我的注册控制器调用验证码模型。 “insert_captcha() 模型函数加载验证码助手,更新验证码 table,并 return 将创建的验证码显示到注册控制器,并将其显示到视图中。

它在 50% 的时间里工作正常,但有时我会收到一大堆 php 错误:

模型摘录:

class captcha_model extends CI_Model {


public function __construct() {
    parent::__construct();
    $this->load->helper('captcha');
}

public function insert_captcha() {

    //captcha creation
    $vals = array(
        'img_path'      => './captcha/',
        'img_url'       =>   base_url() . 'captcha',
        'font_path'     => './assets/fonts/OpenSans-Bold.ttf',
        'img_width'     => '150',
        'img_height'    => 30,
        'expiration'    => 900,
        'word_length'   => 8,
        'font_size'     => 16,
        'img_id'        => 'captcha-pic',
        'pool'          => '0123456789abcdefghijklmnopqrstuvwxyz',
        'colors'        => array(
            'background' => array(255, 255, 255),
            'border' => array(255, 255, 255),
            'text' => array(0, 0, 0),
            'grid' => array(255, 40, 40)
        )
    );
    $captcha = create_captcha($vals);

    //insert captcha into DB
    $captcha_db_params = array(
        'captcha_time'  => $captcha['time'],
        'ip_address'    => $this->input->ip_address(),
        'word'          => $captcha['word']
    );

    $query = $this->db->insert_string('captcha', $captcha_db_params);
    if ( $this->db->query($query) ) { return $captcha;  }           
    else { return false; } 
}
}

控制器摘录

class Register extends CI_Controller {

public $data = array();

public function __construct() {

        parent::__construct();
        $this->load->model('captcha_model');

        if ($this->session->userdata('logged_in')) {  
            $this->data['name'] = $this->session->userdata('name');
            $this->data['id'] = $this->session->userdata('id');
            $this->data['email'] = $this->session->userdata('email');
            $this->data['isAdmin'] = $this->session->userdata('isAdmin');
        }
        if (!empty($this->session->flashdata('error'))) { $this->data['error'] = $this->session->flashdata('error'); }

}

public function index() {

    //captcha creation success
    if ($cap = $this->captcha_model->insert_captcha()) {

        $this->data['title'] = 'Register';
        $this->data['captcha'] = $cap;
        $this->template->load('default', 'register/register', $this->data);

    }

    //not supposed to happen
    else {
        $this->data['error'] = 'Erreur systeme, captcha non genere';
        $this->template->load('default', 'register/register', $this->data);     
    }
}

错误

A PHP Error was encountered

Severity: Notice

Message: **Uninitialized string offset: 36**

Filename: helpers/captcha_helper.php

Line Number: 174

Backtrace:

File: C:\wamp\www\jpweb\models\captcha_model.php Line: 48 Function: create_captcha

File: C:\wamp\www\jpweb\controllers\Register.php Line: 26 Function: insert_captcha

File: C:\wamp\www\index.php Line: 292 Function: require_once A PHP Error was encountered

Severity: Warning

Message: **unpack(): Type C: not enough input, need 1, have 0**

Filename: helpers/captcha_helper.php

Line Number: 174

Backtrace:

File: C:\wamp\www\jpweb\models\captcha_model.php Line: 48 Function: create_captcha

File: C:\wamp\www\jpweb\controllers\Register.php Line: 26 Function: insert_captcha

File: C:\wamp\www\index.php Line: 292 Function: require_once A PHP Error was encountered

Severity: Notice

Message: **String offset cast occurred**

Filename: helpers/captcha_helper.php

Line Number: 206

Backtrace:

File: C:\wamp\www\jpweb\models\captcha_model.php Line: 48 Function: create_captcha

File: C:\wamp\www\jpweb\controllers\Register.php Line: 26 Function: insert_captcha

File: C:\wamp\www\index.php Line: 292 Function: require_once A PHP Error was encountered

Severity: Notice

Message: **Uninitialized string offset: 37**

Filename: helpers/captcha_helper.php

Line Number: 174

Backtrace:

File: C:\wamp\www\jpweb\models\captcha_model.php Line: 48 Function: create_captcha

File: C:\wamp\www\jpweb\controllers\Register.php Line: 26 Function: insert_captcha

File: C:\wamp\www\index.php Line: 292 Function: require_once A PHP Error was encountered

Severity: Warning

Message: **unpack(): Type C: not enough input, need 1, have 0**

Filename: helpers/captcha_helper.php

Line Number: 174

Backtrace:

File: C:\wamp\www\jpweb\models\captcha_model.php Line: 48 Function: create_captcha

File: C:\wamp\www\jpweb\controllers\Register.php Line: 26 Function: insert_captcha

File: C:\wamp\www\index.php Line: 292 Function: require_once A PHP Error was encountered

Severity: Notice

Message: **String offset cast occurred**

Filename: helpers/captcha_helper.php

Line Number: 206

Backtrace:

File: C:\wamp\www\jpweb\models\captcha_model.php Line: 48 Function: create_captcha

File: C:\wamp\www\jpweb\controllers\Register.php Line: 26 Function: insert_captcha

File: C:\wamp\www\index.php Line: 292 Function: require_once

这是一个 CodeIgniter 错误:https://github.com/bcit-ci/CodeIgniter/issues/4427

即将发布的 3.0.5 版本将修复它。