Amir Rachman Amir Rachman - 7 months ago 21
PHP Question

Captcha value different with session user data

I'm trying to create captcha, the image is shown as expected, but when i pass into controller why the string that i input is different with session user data that i set in earlier.

PFB my code.

View.

<?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;
}


Controller (Index)

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);
}


Controller (Process)

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');
}
}


result:

Drd27VZE <--- this one is the captcha
VzU90odU <--- this one is the session user data 'capt'


I've been trying using with separate code (example that i got from google) it works, but i don't know why it's not working with this code.

Answer

Finally i solve my problem,

problem when i create function for captcha i don't have to call again in the process function

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');
    }   
}