验证码值与会话用户数据不同
Captcha value different with session user data
我正在尝试创建验证码,图像按预期显示,但是当我传递到控制器时,为什么我输入的字符串与我之前设置的会话用户数据不同。
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>');?>
控制器(配置)
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'
我一直在尝试使用单独的代码(我从 google 获得的示例)它可以工作,但我不知道为什么它不能使用此代码。
您可以在助手中创建验证码功能
帮手
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 } ?>
终于解决了我的问题,
当我为验证码创建函数时出现问题,我不必在处理函数中再次调用
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');
}
}
我正在尝试创建验证码,图像按预期显示,但是当我传递到控制器时,为什么我输入的字符串与我之前设置的会话用户数据不同。
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>');?>
控制器(配置)
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'
我一直在尝试使用单独的代码(我从 google 获得的示例)它可以工作,但我不知道为什么它不能使用此代码。
您可以在助手中创建验证码功能
帮手
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 } ?>
终于解决了我的问题,
当我为验证码创建函数时出现问题,我不必在处理函数中再次调用
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');
}
}