我在我的智慧结束,不知道为什么我的代码不起作用。一切对我来说都很好..我在用户评论功能中实现了验证码,并使用callback_添加了验证方法。
验证码显示在视图中,我已经转储了会话数据和输入字段数据,它们都在工作。
表单验证也适用于captcha
输入字段,但似乎callback_check_captcha参数不起作用,但函数对我来说似乎很好。
这是我的控制器
function user_review($id = null , $start = 0){
$check_id = $this->mdl_phone->get_phone_feature($id);
if ($check_id == null ) {
$data['phone_model'] = $this->get_phone_models();
$data['feature'] = null;
$data['title'] = 'Nothing Found';
$data['main_content']= 'phone/error';
echo Modules::run('templates/main',$data);
} else{
$data['phone_model'] = $this->get_phone_models();
$data['success'] = null;
$data['errors'] = null;
if($this->input->server("REQUEST_METHOD") === 'POST' ){
$this->load->library('form_validation');
$this->form_validation->set_rules('text','Review','required|xss_clean');
$this->form_validation->set_rules('captcha', 'Captcha','trim|required|callback_check_captcha');
if($this->form_validation->run() == FALSE){
$data['errors'] = validation_errors();
}else{
$user = $this->ion_auth->user()->row();
$user_id = $user->id;
$data = array(
'phone_id' => $id,
'user_id' => $user_id,
'text' => strip_tags($this->input->post('text')),
);
$this->db->insert('user_review' ,$data);
$data['phone_model'] = $this->get_phone_models();
$data['success'] = 'Your Review has been successfully posted ';
$data['errors']= null;
}
}
// Initilize all Data at once by $id
$data['feature'] = $this->mdl_phone->get_phone_feature($id);
//$data['rating'] = $this->mdl_phone->get_user_rating($id);
$data['user_review'] = $this->mdl_phone->get_user_review($id , 5 , $start);
$this->load->library('pagination');
$config['base_url'] = base_url().'phone/user_review/'.$id;
$config['total_rows'] = $this->mdl_phone->get_user_review_count($id);
$config['per_page'] = 5;
$config['uri_segment'] = 4;
$config['anchor_class'] = 'class="page" ';
$this->pagination->initialize($config);
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',
'img_url' => base_url().'captcha/',
'img_width' => 150,
'img_height' => 30,
);
$cap = create_captcha($vals);
$this->session->set_userdata('captcha',$cap['word']);
$data['captcha'] = $cap['image'];
$data['title'] = $this->mdl_phone->get_phone_title($id)." User Review , Rating and Popularity";
$data['main_content'] = 'phone/user_review';
echo Modules::run('templates/main',$data);
}
}
function check_captcha($cap)
{
if($this->session->userdata('captcha') == $cap )
{
return true;
}
else{
$this->form_validation->set_message('check_captcha', 'Security number does not match.');
return false;
}
}
答案 0 :(得分:0)
以下CAPTCHA正常工作,请参阅此代码
$this->load->helper('captcha');
$vals = array(
'img_path' => './captcha/',
'img_url' => base_url() . '/captcha/'
);
$cap = create_captcha($vals);
$data = array(
'captcha_time' => $cap['time'],
'ip_address' => $this->input->ip_address(),
'word' => $cap['word']
);
$this->session->set_userdata($data);
$data['cap_img'] = $cap['image'];
我认为您的图片网址问题或者您没有向captcha文件夹授予文件权限。