我试图创建验证码,图像按预期显示,但是当我进入控制器时为什么我输入的字符串与我之前设置的会话用户数据不同。
PFB我的代码。
查看。
<?php echo $captcha_img;?>
<input type="text" id="captcha" name="captcha" placeholder="Input Code Above">
<?php echo form_error('captcha', '<p class="field_error">', '</p>');?>
Controller(config)
public function captcha_config() {
$vals = array(
'img_path' => './assets/files/captcha/',
'img_url' => base_url().'assets/files/captcha/',
'img_width' => 150,
'img_height' => 30
);
$cap = create_captcha($vals);
$image = $cap['image'];
$this->session->set_userdata('capt',$cap['word']);
//$data['captcha_img'] = $cap['image'];
return $image;
}
控制器(索引)
public function index() {
if ($this->session->userdata('login') == TRUE)
{
redirect('Buser');
}
else
{
$data['title'] = 'Dashboard';
$data['subtitle'] = 'Log In';
$this->captcha_config();
$data['captcha_img'] = $this->captcha_config();
$this->load->view('Backend/login', $data);
}
控制器(流程)
public function process_login() {
$data['title'] = 'Dashboard';
$data['subtitle'] = 'Log In';
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('captcha', 'Captcha', 'required' );
$this->captcha_config();
$data['captcha_img'] = $this->captcha_config();
if ($this->form_validation->run() == TRUE)
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$captcha = $this->input->post('captcha');
echo $captcha;
echo "<br>";
echo $this->session->userdata('capt');
}
}
结果:
Drd27VZE <--- this one is the captcha
VzU90odU <--- this one is the session user data 'capt'
我一直在尝试使用单独的代码(我从谷歌获得的示例)它有效,但我不知道它为什么不使用此代码。
答案 0 :(得分:0)
您可以在帮助程序中创建验证码功能
辅助
function generate_captcha()
{
$options = array(
'img_path' => 'img/captcha/',
'img_url' => base_url().'img/captcha/',
'img_width' => '150',
'img_height'=> '50',
'expiration'=> 3600,
'word' => generate_random_password(4)
);
return create_captcha($options);
}
function generate_random_password( $max_length = 8 )
{
$pass = '';
$dict = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
$dict_size = strlen($dict);
for($i = 0;$i<$max_length;$i++){
$pass .= $dict[rand(0,$dict_size-1)];
}
return strtoupper($pass);
}
在您的视图中,如果用户输错了用户,则调用generate_captcha
<?php if( ! empty( $attempts_number ) && ((int)$attempts_number >= 3) ) { ?>
<?php $captcha = generate_captcha(); ?>
<?php $this->session->set_userdata(array(
'captchaword' => $captcha['word']
)); ?>
<?php echo $captcha['image']; ?>
<label><?php echo $this->lang->line('captcha_text'); ?>:</label>
<?php echo form_input(array('class'=>'required','maxlength'=>4,'name'=>'captcha'));?>
<?php } ?>
答案 1 :(得分:0)
最后我解决了我的问题,
问题当我为验证码创建功能时我不必再在过程函数中再次调用
public function process_login() {
$data['title'] = 'Dashboard';
$data['subtitle'] = 'Log In';
$this->form_validation->set_rules('username', 'Username', 'required');
$this->form_validation->set_rules('password', 'Password', 'required');
$this->form_validation->set_rules('captcha', 'Captcha', 'required' );
**$this->captcha_config(); <---- I Remove this one
$data['captcha_img'] = $this->captcha_config(); <---- and also remove this one**
if ($this->form_validation->run() == TRUE)
{
$username = $this->input->post('username');
$password = $this->input->post('password');
$captcha = $this->input->post('captcha');
echo $captcha;
echo "<br>";
echo $this->session->userdata('capt');
}
}