Replace reCaptcha by another captcha system

Posted by | April 05, 2013 | Premium URL Shortener, Tutorials | 15 Comments




In this tutorial, I will show you how to replace the  reCaptcha plugin of Premium URL Shortener by another captcha system because as good as reCaptcha might be, there are plenty of solutions that are much fun and pleasant.

Step 1: Choose your new CAPTCHA system

Before we start replacing reCaptcha you must first determine which CAPTCHA system you want to use. There are plenty of options but my favorite ones are Solve Media and Are You A Human?

For this tutorial, we will work with Solve Media but you can still use the same principle for other CAPTCHA system.

Step 2: Register and grab your API key

Most CAPTCHA requires you to register your site and generate an API key(s). Some of them absolutely require the API key and do not function otherwise.

Step 3: Replace the existing CAPTCHA system

Before we can use the CAPTCHA system, we must first download the library provided by the developer. In the case of Solve Media, I have tweaked the library and you can download it here . Save it as “Solvemedia.php” and then put in your includes folder.

Open that file and find the following piece of code on line 48-50 and fill them up using the keys provided by Solve Media.

define("SV_PRIVKEY", ""); 
define("SV_HASHKEY", "");
define("SV_CHALLENGE", "");

Step 4: Implement it to Premium URL Shortener

Please backup the files you are editing in case something goes wrong.

4.1 Modify Short.class.php

So now we need to implement it. Let’s start with the front page. Open Short.class.php and go to line 40. You will notice the following code:

require_once("Recaptcha.php");

if(empty($array["recaptcha_response_field"]) && !$error) {
	$error = array('error' => 1, 'msg' => e('Please enter the reCAPTCHA.'),'html'=>'captcha'); 
}else{ 
           $resp = recaptcha_check_answer ($this->config["captcha_private"],$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]); 
            if (!$resp->is_valid && !$error) { 
                  $error = array('error' =>1, 'msg' =>; e("The reCAPTCHA wasn't entered correctly. Please try it again.")."<script type='text/javascript'>Recaptcha.reload();</script>",'html'=>;'captcha'); 
             } 
}

You have to replace the code above by the code below.

require_once("Solvemedia.php");
  if(empty($array["adcopy_response"]) && !$error) {
	$error = array('error' =>1, 'msg' => e('Please enter the CAPTCHA.'),'html'=>'captcha');	
  }else{
	$resp = solvemedia_check_answer(SV_PRIVKEY,$_SERVER["REMOTE_ADDR"],$array["adcopy_challenge"],$array["adcopy_response"],SV_HASHKEY);
	if (!$resp->is_valid && !$error) {
		 $error = array('error' => 1, 'msg' => e("The CAPTCHA wasn't entered correctly. Please try it again.")."<script type='text/javascript'>ACPuzzle.reload();</script>",'html'=>'captcha'); 
        } 
}

Then open index.php and replace

require_once("includes/Recaptcha.php");

by

require_once("includes/Solvemedia.php");

And finally open template/index.php and on line 57 replace

echo recaptcha_get_html($config["captcha_public"]);

 by

echo solvemedia_get_html(SV_CHALLENGE);

4.2 Modify user.php, includes/User.class.php and template/register.php

Don’t forget that you have to do the same for the registration page. In that case, open user.php and replace

require_once("includes/Recaptcha.php");

by

require_once("includes/Solvemedia.php");

and then open template/register.php and replace

echo recaptcha_get_html($config["captcha_public"]);

 by

echo solvemedia_get_html(SV_CHALLENGE);

Finally open User.class.php, go to line 51 and replace

if(empty($array["recaptcha_response_field"])) {
	$error.= e('Please enter the reCAPTCHA.');	
}else{
  $resp = recaptcha_check_answer ($this->config["captcha_private"],$_SERVER["REMOTE_ADDR"],$_POST["recaptcha_challenge_field"],$_POST["recaptcha_response_field"]);
  if (!$resp->is_valid) {
    $error .= e("The reCAPTCHA wasn't entered correctly. Please try it again.");
  }				
}

by

if(empty($array["adcopy_response"])) {
	$error.= e('Please enter the reCAPTCHA.');	
}else{
  $resp = solvemedia_check_answer(SV_PRIVKEY,$_SERVER["REMOTE_ADDR"],$array["adcopy_challenge"],$array["adcopy_response"],SV_HASHKEY);
  if (!$resp->is_valid) {
    $error .= e("The reCAPTCHA wasn't entered correctly. Please try it again.");
  }				
}

If you have done everything quickly, then it should all work fine. If it doesn’t work or there is an error or you are simply confused, leave a comment below and I will help you out.

15 Comments

Leave a Reply

Your email address will not be published.