PHP scripts and codes from $6

Stop Spam On Your Website By Implementing Google reCAPTCHA PHP Script


A friend of mine complained to me about him getting lots of spam messages and replies in his forum website. He said not only are they annoying, but contain adult and porn links. I decided to implement a simple captcha to help fight spam using google reCAPTCHA.
If you are in same problem, follow the steps below to code and implement a captcha to help fight spam in your website.

  1. Head over to google reCAPTCHA page
  2. Sign up by submitting your website URL
  3. You will be given a public and private key
  4. Downlnad the google reCAPTCHA here
  5. Extract the recaptchalib.php and upload to the root folder of your site
  6. Next to do is implementing the captcha in your site as outline below:-
    • Add the below code to your html <form>

      require_once('recaptchalib.php');
         $publickey = "your_public_key_here";  // you got this from signup page
        echo recaptcha_get_html($publickey);
      

      Example

      <form method="post" action="verify.php">
          <?php
           require_once('recaptchalib.php');
         $publickey = "your_public_key_here";  // you got this from signup page
        echo recaptcha_get_html($publickey);
        ?>
        <input type="submit" />
       </form>
      

      Don’t forget to set $publickey by replacing your_public_key_here with your API public key.

    • Next, we need to add the below code in our html form file (verify.php).
      <?php
        require_once('recaptchalib.php');
        $privatekey = "your_private_key_here";
        $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
      }
      ?>
      

      Also make sure your form is set to get the form variables using $_POST, instead of $_REQUEST, and that the form itself is using the POST method.

If you have done the steps above successfully, congratulation! You won’t be haunted again by spam.
If you encounter difficulties implementing this or have any contribution to make, i will be glad to hear your comments.

Don’t Want To Miss Our Update, Subscribe Now
About collizo4sky

Agbonghama Collins is a webdeveloper and an internet geek. I love writing and sharing the little idea & information i know. Hope you enjoyed my Article.

You can get in touch with him via these modes: Follow him on Twitter, add him on Facebook or on Google+

  • http://www.baseloaded.com lastdon02

    Gud job bt guy sum people day buypass dat tin o shay u no?

  • http://wapden.net collizo4sky

    It can be bypassed. Google reCAPTCHA is used by facebook, twitter, yahoo etc to stop spam.

  • http://www.bloghandy.com Tayomi

    Nice one. Time to implement dis on ma new script.