0

会话类URL 助手是自动加载的。

控制器:

$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('captcha');
$vals = array(
    'img_path' => './captcha/',
    'img_url' => base_url() . 'captcha/',
    'img_width' => '250',
    'img_height' => '50');

$cap = create_captcha($vals);
$datacap = array('captcha_time' => $cap['time'], 'word' => $cap['word']);
$this->session->set_userdata($datacap);

$this->form_validation->set_rules('captcha_image', 'Captcha',
    'required|callback_captchacheck');
if ($this->form_validation->run() == false)
{
    $datatw['image'] = $cap['image'];
    $datatw['file_view'] = "register";
    $this->load->view('home', $datatw);
} else { // }

打回来:

public function captchacheck($capt)
{
    $captss = $this->session->userdata('word');
    if ($captss == $capt)
    {
        return true;
    } else
    {
        $this->form_validation->set_message('captchacheck',
            'Security number does not match.');
        return false;
    }
}

它返回安全号码不匹配

echo $this->session->userdata('word');查看和复制粘贴,但它不起作用。


错误已修复!

对不起,我搬家了:

$vals = array(
        'img_path' => './captcha/',
        'img_url' => base_url() . 'captcha/',
        'img_width' => '250',
        'img_height' => '50');

    $cap = create_captcha($vals);
    $datacap = array('captcha_time' => $cap['time'], 'word' => $cap['word']);
    $this->session->set_userdata($datacap);

至:

if ($this->form_validation->run() == false){
    //
}

它运作良好:

$this->load->helper('form');
$this->load->library('form_validation');
$this->load->helper('captcha');

$this->form_validation->set_rules('captcha_image', 'Captcha',
    'required|callback_captchacheck');
if ($this->form_validation->run() == false)
{
    $vals = array(
        'img_path' => './captcha/',
        'img_url' => base_url() . 'captcha/',
        'img_width' => '250',
        'img_height' => '50');

    $cap = create_captcha($vals);
    $datacap = array('captcha_time' => $cap['time'], 'word' => $cap['word']);
    $this->session->set_userdata($datacap);
    $datatw['image'] = $cap['image'];
    $datatw['file_view'] = "register";
    $this->load->view('home', $datatw);
} else
{  }
4

1 回答 1

1

调用回调时是否加载了会话?

$this->output->enable_profiler();

或者

print $this->session->userdata('word'); // inside your callback

这不是因为会话仅适用于下一个服务器请求。

解决方案:将第二个参数传递给回调,不要为此使用会话数据。

$this->form_validation->set_rules('captcha_image', 'Captcha',
    'required|callback_captchacheck['. $datacap ['word'] .']');

(需要 CI 2.1.0+)

于 2013-05-07T14:14:54.213 回答