Codeigniter captcha

codeigniter captcha codexworld
create_captcha not working in codeigniter
codeigniter math captcha
codeigniter captcha github
how to change captcha image in codeigniter
botdetect captcha codeigniter
refresh captcha codeigniter
codeigniter 2 captcha example

I ran into a small problem what i cant figure out.

I would liked to use the captcha helper without database but nothing seems to be working.

in the construct i created a variable which generates a random string

   public function __construct()
   {
      parent::__construct();
      $this->rand = random_string('numeric', 4);
   }

passed this variable to the captcha values like this:

 $vals = array(
     'word'   => $this->rand,
     'img_path'  => './captcha/',
     'img_url'   => base_url() . 'captcha/',
     'font_path'    => './system/fonts/texb.ttf',
     'img_width'    => '150',
     'img_height' => 30,
     'expiration' => 7200
     );

 $cap = create_captcha($vals);

and wanted to validate it with a callback function like this

   function captcha_check()
   {
      if($this->input->post('code') != $this->rand)
      {
         $this->form_validation->set_message('captcha_check', 'Wrong captcha code, hmm are you the Terminator?');
         return false;
      }
   }

And nothing works, the problem is, captcha code is show with the image, the problem is the validation, if i type in the correct numbers in my captcha field it always showing me the error, no mather what i type in, could please someone give me a hint?

html:

  <label for="code">Count please </label>
      <?php echo $rand; ?>
  <input type="text" id="code" name="code" class="span3" />

validation line:

$this->form_validation->set_rules('code', 'Captcha', 'required|callback_captcha_check');

Modify your callback function like this:

   function captcha_check()
   {
      if($this->input->post('code') != $this->rand)
      {
         $this->form_validation->set_message('captcha_check', 'Wrong captcha code, hmm are you the Terminator?');
         return false;
      }
      return true;
   }

EDIT:

You need to move this line $this->rand = random_string('numeric', 4); out of the constructor because on each load of the controller it generates a new string, resulting in different values on the captcha initial load and the captcha verification. Simply place it before the $vals initialization.

CAPTCHA Helper : CodeIgniter User Guide, $cap = create_captcha($vals); echo $cap['image'];. The captcha function requires the GD image library. Only the img_path and img_url are required. If  The shortest path to protecting your CodeIgniter forms with BotDetect Captcha. BotDetect CodeIgniter 3 CAPTCHA Integration Quickstart.

 * Example of captcha validation without database
 * Instead of it used session to store captcha value
 * The images will be deleted after the use

--

public function index()
    {   
        $this->load->helper(array('form', 'url','captcha'));
        $this->load->library('form_validation');

       $this->form_validation->set_error_delimiters('<div class="error">', '</div>');
       $this->form_validation->set_rules('username', 'Username', 'trim|required|xss_clean');
       $this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');
       $this->form_validation->set_rules('captcha', 'Captcha', 'callback_validate_captcha');

    if($this->form_validation->run() == FALSE)
       {

        $original_string = array_merge(range(0,9), range('a','z'), range('A', 'Z'));
        $original_string = implode("", $original_string);
        $captcha = substr(str_shuffle($original_string), 0, 6);

         //Field validation failed.  User redirected to login page
        $vals = array(
                'word' => $captcha,
                'img_path' => './captcha/',
                'img_url' => 'http://mycodeignitor.org/captcha/',
                'font_path' => BASEPATH.'fonts/texb.ttf',
                'img_width' => 150,
                'img_height' => 50,
                'expiration' => 7200
        );

        $cap = create_captcha($vals);
        $data['image'] = $cap['image'];

        if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image']))
            unlink(BASEPATH."../captcha/".$this->session->userdata['image']);

        $this->session->set_userdata(array('captcha'=>$captcha, 'image' => $cap['time'].'.jpg'));
        $this->load->view('index_index',$data);
       }
       else
       {
            if(file_exists(BASEPATH."../captcha/".$this->session->userdata['image']))
                unlink(BASEPATH."../captcha/".$this->session->userdata['image']);

            $this->session->unset_userdata('captcha');
            $this->session->unset_userdata('image');
            redirect('home', 'refresh');
       }



}

public function validate_captcha(){
    if($this->input->post('captcha') != $this->session->userdata['captcha'])
    {
        $this->form_validation->set_message('validate_captcha', 'Wrong captcha code, hmm are you the Terminator?');
        return false;
    }else{
        return true;
    }

}

Easily Integrate CAPTCHA into CodeIgniter Projects, Integrate CAPTCHA into CodeIgniter projects to add a great layer of security for your project. This tutorial highlights the easy CAPTCHA  The _generateCaptcha() method will generate captcha image and will place generated image inside captcha_images directory as configurated and you must create captcha_images directory in your codeigniter project root directory and change its permission to 777 or 666.

$vals = array(
     'word'   => $this->rand,
     'img_path'  => './captcha/',
     'img_url'   => base_url() . 'captcha/',
     'font_path'    => './system/fonts/texb.ttf',
     'img_width'    => '150',
     'img_height' => 30,
     'expiration' => 7200
     );

$cap = create_captcha($vals);
$this->session->set_userdata($cap);

 function captcha_check()
   {
      if($this->input->post('code') == $this->session->userdata('word'))
      {
return true;
}else{
         $this->form_validation->set_message('captcha_check', 'Wrong captcha code, hmm are you the Terminator?');
         return false;
      }
   }

Using codeigniter 3 CAPTCHA helper in forms, Hi, I will show you a very simple way to use codeigniter 3 CAPTCHA helper when​ Tagged with codeigniter, captcha, helper, form. CodeIgniter provides a CAPTCHA helper to generate random code and create CAPTCHA image. CodeIgniter CAPTCHA Helper contains functions that help to generate CAPTCHA images with various customization options. Here we’ll provide the example code to implement CAPTCHA functionality in CodeIgniter. The following functionality will be implemented in this simple PHP captcha script for CodeIgniter. Create and display captcha image. Get user input and submit for comparison.

How to Implement Captcha in CodeIgniter using Captcha Helper , CodeIgniter 3 basic Captcha validation - PHP code example - source code listing and explanation. To prevent website from spamming, one can use CodeIgniter's CAPTCHA Helper file that will assist you to create CAPTCHA images. We have use an CodeIgniter Captcha example to demostrate it.

CodeIgniter 3 Basic BotDetect CAPTCHA Example, This blog post demonstrate you, how to implement simple captcha using CodeIgniter captcha helper file. Captcha is basically a random generated captcha string  CodeIgniter 3 basic Captcha validation - PHP code example - source code listing and explanation.

CodeIgniter Captcha Helper Example, In this tutorial, I will show you integrating Captcha in your CodeIgniter application. We gonna use CI's builtin captcha helper and contains functions that assist in  CodeIgniter-reCAPTCHA. The FREE anti-abuse service. Easy to add, advanced security, accessible to wide range of users and platforms. This package is compliant with PSR-1 and PSR-2. What is reCAPTCHA? reCAPTCHA is a free service that protects your site from spam and abuse. It uses advanced risk analysis engine to tell humans and bots apart.

Comments
  • just noticed that, still the same (removed)