Build Captcha in CodeIgniter using Captcha Helper
Before started to implement the Build Captcha in CodeIgniter using Captcha Helper, look files structure:
- codeigniter-send-email-with-attachment
- application
- config
- constants.php
- routes.php
- controllers
- Captcha.php
- views
- captcha
- index.php
- templates
- header.php
- footer.php
- captcha
- config
- system
- index.php
- assets
- css
- style.css
- uploads
- captcha_images
- application
Step 1: Load “captcha ” class in controller.
1 2 3 4 |
<?php // load captcha helper $this->load->helper('captcha'); ?> |
Step 2: Create a controller file
Create a controller file named
Captcha.php
inside “application/controllers” folder. - The controller handles the captcha process.
__construct()
– Loads the required library and helper.index()
– load form and generate Captcha Image.refresh()
– refresh Captcha.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); /** * Version: 1.0.0 * * Description of Captcha Controller * * @author TechArise Team * * @email info@techarise.com * **/ // Captcha class class Captcha extends CI_Controller { function __construct() { parent::__construct(); // Load the captcha helper $this->load->helper('captcha'); } // index public function index(){ $data = array(); $data['metaDescription'] = 'Build Captcha in CodeIgniter using Captcha Helper'; $data['metaKeywords'] = 'Build Captcha in CodeIgniter using Captcha Helper'; $data['title'] = "Build Captcha in CodeIgniter using Captcha Helper - TechArise"; $data['breadcrumbs'] = array('Build Captcha in CodeIgniter using Captcha Helper' => '#'); // If captcha form is submitted if($this->input->post('submit')) { $inputCaptcha = $this->input->post('captcha'); $sessCaptcha = $this->session->userdata('captchaCode'); if($inputCaptcha === $sessCaptcha){ $data['msg'] = '<div class="alert alert-success alert-dismissible"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <strong>Success!</strong> Captcha code matched. </div>'; }else{ $data['msg'] = '<div class="alert alert-danger alert-dismissible"> <a href="#" class="close" data-dismiss="alert" aria-label="close">×</a> <strong>Failed!</strong> Captcha does not match, please try again. </div> '; } } // Captcha configuration $config = array( 'img_path' => BASH_PATH.'assets/uploads/captcha_images/', 'img_url' => base_url().'assets/uploads/captcha_images/', 'font_path' => BASH_PATH.'system/fonts/texb.ttf', 'img_width' => 170, 'img_height' => 55, 'expiration' => 7200, 'word_length' => 6, 'font_size' => 25, 'colors' => array( 'background' => array(171, 194, 177), 'border' => array(10, 51, 11), 'text' => array(0, 0, 0), 'grid' => array(185, 234, 237) ) ); $captcha = create_captcha($config); // Unset previous captcha and set new captcha word $this->session->unset_userdata('captchaCode'); $this->session->set_userdata('captchaCode', $captcha['word']); // Pass captcha image to view $data['captchaImg'] = $captcha['image']; // Load the view $this->load->view('captcha/index', $data); } // refresh public function refresh(){ // Captcha configuration $config = array( 'img_path' => BASH_PATH.'assets/uploads/captcha_images/', 'img_url' => base_url().'assets/uploads/captcha_images/', 'font_path' => BASH_PATH.'system/fonts/texb.ttf', 'img_width' => 170, 'img_height' => 55, 'expiration' => 7200, 'word_length' => 6, 'font_size' => 25, 'colors' => array( 'background' => array(171, 194, 177 ), 'border' => array(10, 51, 115), 'text' => array(0, 0, 0), 'grid' => array(185, 234, 237) ) ); $captcha = create_captcha($config); // Unset previous captcha and set new captcha word $this->session->unset_userdata('captchaCode'); $this->session->set_userdata('captchaCode',$captcha['word']); // Display captcha image echo $captcha['image']; } } ?> |
Step 3: Create a view
Create a view file named
index.php
inside “application/views/captcha” folder
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
<?php $this->load->view('templates/header');?> <section class="showcase"> <div class="container"> <div class="pb-2 mt-4 mb-2 border-bottom"> <h2>Build Captcha in CodeIgniter using Captcha Helper</h2> </div> <div class="row"> <div class="col-md-12 gedf-main"> <?php if(!empty($msg)) { echo $msg; } ?> <form method="post"> <div class="row"> <label class="control-label" style="padding-left: 18px;">Enter captcha code:</label> <div class="col-sm-3"> <input type="text" class="form-control" name="captcha" /> </div> <div class="col-sm-3 text-left"> <button type="submit" name="submit" class="btn btn-primary btn-md" value="submit">Submit</button> </div> </div> </form> <br> <p id="captcha-img"><?php echo $captchaImg; ?></p> <p>Can't read the image? click <a href="javascript:void(0);" class="refresh-captcha">here</a> to refresh.</p> </div> </div> </div> </section> <?php $this->load->view('templates/footer');?> <script> jQuery(document).ready(function(){ jQuery('a.refresh-captcha').on('click', function(){ jQuery.get('<?php print base_url().'captcha/refresh'; ?>', function(data) { jQuery('p#captcha-img').html(data); }); }); }); </script> |
Step 7: You can add Javascript code for refresh captcha
1 2 3 4 5 6 7 8 9 |
<script> jQuery(document).ready(function(){ jQuery('a.refresh-captcha').on('click', function(){ jQuery.get('<?php print base_url().'captcha/refresh'; ?>', function(data) { jQuery('p#captcha-img').html(data); }); }); }); </script> |
Step 4:Create a view(header)
Create a view file named header.php inside “application/views/templates” folder.
This view contains the header section of the webpage. The Bootstrap library is used to provide a better UI, so, include it in the header section.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
<?php defined('BASEPATH') OR exit('No direct script access allowed'); ?> <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no"> <meta name="description" content=""> <meta name="author" content=""> <title><?php print $title; ?></title> <link rel="icon" type="image/ico" href="<?php print HTTP_IMAGE_PATH; ?>favicon.ico"> <!-- Bootstrap core CSS --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/css/bootstrap.min.css" /> <!-- Custom fonts for this template --> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/5.7.2/css/all.min.css" /> <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/simple-line-icons/2.4.1/css/simple-line-icons.css" /> <link href="https://fonts.googleapis.com/css?family=Lato:300,400,700,300italic,400italic,700italic" rel="stylesheet" type="text/css"> <!-- Custom styles for this template --> <link href="<?php print HTTP_CSS_PATH; ?>style.css" rel="stylesheet"> </head> <body> <!-- Navigation --> <nav class="navbar navbar-expand-lg navbar-dark bg-dark static-top header-bg-dark" style="background: ##FFFFFF!;"> <div class="container"> <a class="navbar-brand font-weight-bold" href="https://techarise.com"><h1>Tech Arise</h1></a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarResponsive" aria-controls="navbarResponsive" aria-expanded="false" aria-label="Toggle navigation"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarResponsive"> <ul class="navbar-nav ml-auto"> <li class="nav-item active"> <a class="nav-link" href="https://techarise.com">Home <span class="sr-only">(current)</span> </a> </li> <li class="nav-item"> <a class="nav-link" href="https://techarise.com/php-free-script-demos/">Live Demo</a> </li> </ul> </div> </div> </nav> |
Step 5: Create a view(footer)
Create a view file named footer.php inside “application/views/templates” folder.
This view contains the footer section of the webpage.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
<!-- Footer --> <footer class="footer bg-light footer-bg-dark"> <div class="container"> <div class="row"> <div class="col-lg-6 h-100 text-center text-lg-left my-auto"> <ul class="list-inline mb-2"> <li class="list-inline-item"> <a href="#">About</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Contact</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Terms of Use</a> </li> <li class="list-inline-item">⋅</li> <li class="list-inline-item"> <a href="#">Privacy Policy</a> </li> </ul> <p class="text-muted small mb-4 mb-lg-0">Copyright © 2011 - <?php print date('Y', time());?> <a href="https://techarise.com/">TECHARISE.COM</a> All rights reserved.</p> </div> <div class="col-lg-6 h-100 text-center text-lg-right my-auto"> <ul class="list-inline mb-0"> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-facebook fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item mr-3"> <a href="#"> <i class="fab fa-twitter-square fa-2x fa-fw"></i> </a> </li> <li class="list-inline-item"> <a href="#"> <i class="fab fa-instagram fa-2x fa-fw"></i> </a> </li> </ul> </div> </div> </div> </footer> <!-- Bootstrap core JavaScript --> <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.3.1/jquery.min.js"></script> <script src="https://cdnjs.cloudflare.com/ajax/libs/twitter-bootstrap/4.3.1/js/bootstrap.bundle.min.js"></script> </body> </html> |