Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • How to add Captcha with CakePHP

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 154
    Comment on it

    We can add captcha for blocking spams robots accessing our site or application. We can add captcha in our web application and website by using the below code ->

    <?php
    App::uses('AppController', 'Controller');
    class PagesController extends AppController {
        public $name = 'Pages';
        public $uses = array();
        public function display() {
    
        if (!empty($this->request->data)) { // this isused to save the form
    
          session_start(); //        start session so we can access the data saved by captcha.php */
    
          if ($this->request->data['Pages']['captcha'] == $this->Session->read('captcha')) {
          $this->set('pass', true);  /* set $pass for view to true */
    
          } else { 
    
            $this->set('pass', false);
          }
        }
    
    
        $this->render('home');// display home file under view folder contains forms and captcha
        }
    }
    

    make a new home.ctp in view folder and paste the below code

    <?php
    /* access control not performed yet */
    if (!isset($pass)) {
    echo $this->Form->create('Pages');
    ?>
    <p>Enter the words displayed in image</p>
    <!-- when browser gets captcha.php which generates image, proper value for captcha will be saved in session -->
    <img src="/app/webroot/captcha/captcha.php" id="captcha" /><br/>
    <?php
    echo $this->Form->input('captcha');
    echo $this->Form->submit('Let me in');
    } else { 
      if ($pass) {
      ?>
      <p style="color: green">OK. You're loggedin.</p>
      <button onclick="document.location='/'">Go back</button>
      <?php
      } else {
      ?>
      <p style="color: red">Go away spammer!</p>
      <?php
      }
    }
    ?>
    

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: