1
votes

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.

2

2 Answers

0
votes

you can create a captcha function in helper

Helper

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

In your view, you call a generate_captcha if the user put a wrong user ou pass

<?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 } ?>
0
votes

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