Movatterモバイル変換


[0]ホーム

URL:


Skip to content
DEV Community
Log in Create account

DEV Community

Cover image for Using codeigniter 3 CAPTCHA helper in forms
fadilxcoder
fadilxcoder

Posted on

     

Using codeigniter 3 CAPTCHA helper in forms

Hi,

I will show you a very simple way to use codeigniter 3 CAPTCHA helper when submitting a form in your web application.

So... Ready, Set, Code !

For this, we will be using Welcome controller and welcome_message view. So the first step is to edit the code in the "index" method in "Welcome" controller and add 3 things.

$this->load->library('form_validation');$this->load->helper('captcha');$this->load->library('session');
Enter fullscreen modeExit fullscreen mode
  1. We load the form validation library in order validate the data when the user submit a form on the webpage.
  2. We load the captcha helper that we will configure afterwards.
  3. We will be using the session library in order to store the captcha value.

Now we will have to configure the CAPTCHA helper

$vals=array('word'=>rand(1,999999),'img_path'=>'./assets/captcha/images/','img_url'=>base_url('assets').'/captcha/images/','font_path'=>base_url('assets').'/captcha/fonts/XYZ.ttf','img_width'=>'150','img_height'=>30,'word_length'=>8,'colors'=>array('background'=>array(255,255,255),'border'=>array(255,255,255),'text'=>array(0,0,0),'grid'=>array(255,75,100)));
Enter fullscreen modeExit fullscreen mode

IMPORTANT NOTICE !

I forgot to mention before, you will have to set your project file structure as below if your configuration is like above.

your_ci_project│└───application│└───system│└───tests│└───index.php│└───.htaccess│└───assets    │    └───captcha           │           └───images           │           └───fonts                 │                 └───XYZ.ttf
Enter fullscreen modeExit fullscreen mode

You can find more details onCodeigniter User Guide.

After configuring it, call the function that will create the captcha and finally sending it to the "welcome_message.php" view.

$data['captcha']=create_captcha($vals);$this->load->view('welcome_message',$data);
Enter fullscreen modeExit fullscreen mode

To see the captcha in your view, just add this piece of code.

echo$captcha['image']
Enter fullscreen modeExit fullscreen mode

I hope everything is okay till now.

Time to validate !

In this part we will code the part where the user click on a submit button on the webpage and the controller will validate the captcha.

Let say we have a form in our view with a button in it.

<?php echo $captcha['image'] ?><inputtype="text"name="captcha"><inputtype="hidden"value="<?php echo $captcha['word'] ?>"name="code"><buttonname="submit_contact">Send</button>
Enter fullscreen modeExit fullscreen mode

As you can see in the above code, I have a input tag of type hidden whereby I stored the captcha string and when submitting the form, this data is also sent to the controller.

In order to avoid creating multiple methods, I will use theindex one but will add anif/else conditional statement to check if a user submit a form. After that we will process the data.

if(isset($_POST['submit_contact'])):$this->session->set_userdata('captcha_answer',$this->input->post('code'));$this->form_validation->set_rules('captcha','Captcha','required|integer|callback_check_captcha');if($this->form_validation->run()==TRUE):$this->session->set_flashdata('positive','CAPTCHA VALIDATED SUCCESSFULLY');redirect(site_url());endif;endif;
Enter fullscreen modeExit fullscreen mode

As you can see above, there is a callback for the submitted captcha in order to check whether it is the same or not as in the image.

publicfunctioncheck_captcha($string){if($string!=$this->session->userdata('captcha_answer')):$this->form_validation->set_message('check_captcha','captcha incorrect');returnfalse;else:returntrue;endif;}
Enter fullscreen modeExit fullscreen mode

After that, if you want you can destroy the session forcaptcha_answer.

That's it!

Top comments(2)

Subscribe
pic
Create template

Templates let you quickly answer FAQs or store snippets for re-use.

Dismiss
CollapseExpand
 
sreenivas7463 profile image
Sreenivas7463
PHP, Codeigniter, Wordpress, Bootstrap and Mysql
  • Location
    Hyderabad
  • Work
    Webdeveloper at ABC
  • Joined
• Edited on• Edited

How to change font of captcha ?

CollapseExpand
 
iamshareque profile image
Shareque
I'm a full stack developer
  • Location
    Pune India
  • Work
    Web Developer
  • Joined

Thanks for this, but I face an issue with directory permission then correct it

Are you sure you want to hide this comment? It will become hidden in your post, but will still be visible via the comment'spermalink.

For further actions, you may consider blocking this person and/orreporting abuse

Web Architect && App Developer - 🎮 💻 🎧 ☕
  • Location
    127.0.0.1
  • Work
    Web Architect
  • Joined

More fromfadilxcoder

DEV Community

We're a place where coders share, stay up-to-date and grow their careers.

Log in Create account

[8]ページ先頭

©2009-2025 Movatter.jp