You may change
$cap=create_captcha();
to
$cap = create_captcha($vals);
and also change
var_dump($cap['image']);
to echo $cap['image'];
codeigniter:fail to create captcha
-
21-06-2023 - |
Question
I am using captcha and I want to create a captcha using the cpatcha helper!I read the user guide and write a piece of code,but it fails to create an captcha,also here is no error displayed on the page,so I just can't find where I am wrong,my code is as follows:
public function captcha(){
$this->load->helper('captcha');
$vals=array(
'word'=>'0123456789',
'img_path'=>base_url('asserts/cpatcha').'/',
'img_url'=>base_url('asserts/cpatcha').'/',
'img_width'=>150,
'img_height'=>30,
'expiration'=>7200
);
var_dump($vals);
$cap=create_captcha($vals);
var_dump($cap['image']);
}
as I print the two variables,the result displayed on the page is as follows:
array (size=6)
'word' => string '0123456789' (length=10)
'img_path' => string 'http://wrecruit.tudouya.com/asserts/cpatcha/' (length=44)
'img_url' => string 'http://wrecruit.tudouya.com/asserts/cpatcha/' (length=44)
'img_width' => int 150
'img_height' => int 30
'expiration' => int 7200
null
Solution
OTHER TIPS
I solve this problem by myself and I post my solution at here,hope this can help others!
at first,the reason why the captcha can't display correctly due to the uncorrect config.it's this:'img_path'=>base_url('asserts/cpatcha').'/',
.
I read the source code of captcha in ci and I find in the line 66-69,it will check if the img_path is a dir,just as follows:
if ( ! @is_dir($img_path))
{
return FALSE;
}
in my config,I set the img_path to "base_url('asserts/cpatcha').'/'",this is not a dir,so it will fail to create the captcha.
the way to solve this is just setting the correct path which will store the captcha image.
hope this can help you!