会话类和 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
{ }
答案 0 :(得分: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+要求)