Captcha | Captcha Code | PHP Captcha

Google Captcha

A CAPTCHA is a type of challenge-response test used in computing as an attempt to ensure that the response is generated by a human being. In other words, CAPTCHA is a security mechanism that ensures visitor to your websites are human and are not automated bots clicking on your sites.

There are lot of paid captcha sites, but Google provide CAPTCHA services free. Just login to your Google account and visit the following URL:

https://www.google.com/recaptcha/admin/list

Click on “Add a New Site”

code for captcha

Just type the URL of the website where you are planning to use your CAPTCHA.

captcha code

Once done, it will generate one “Private Key” and one “Public Key”.

php captcha

To use CAPTCHA for PHP, use the following library:

http://code.google.com/p/recaptcha/downloads/list?q=label:phplib-Latest

You will be using only “recaptchalib.php” file. Save the file in your webserver and create a form “form.html” with following code. Dont forget to replace your_public_key with the “Public Key” that you got in last step.

  <html>
    <body> <!-- the body tag is required or the CAPTCHA may not show on some browsers -->
      <!-- your HTML content -->
      <form method="post" action="verify.php">
        <?php
          require_once('recaptchalib.php');
          $publickey = "your_public_key"; // you got this from the signup page
          echo recaptcha_get_html($publickey);
        ?>
        <input type="submit" />
      </form>
      <!-- more of your HTML content -->
    </body>
  </html>

To verify the CAPTCHA we need to create “verify.php” in the same folder with following code and you are all set.

 <?php
  require_once('recaptchalib.php');
  $privatekey = "your_private_key";
  $resp = recaptcha_check_answer ($privatekey,
                                $_SERVER["REMOTE_ADDR"],
                                $_POST["recaptcha_challenge_field"],
                                $_POST["recaptcha_response_field"]);
  if (!$resp->is_valid) {
    // What happens when the CAPTCHA was entered incorrectly
    die ("The reCAPTCHA wasn't entered correctly. Go back and try it again." .
         "(reCAPTCHA said: " . $resp->error . ")");
  } else {
    // Your code here to handle a successful verification
  }
  ?>

You can get further instructions for: PHPWordPress, andMediaWiki.

Leave a Reply

You must be Logged in to post comment.

© 2014 Technology Blog. All rights reserved. Site Admin · Entries RSS · Comments RSS