function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
ForceComForceCom 

Captcha ---Help Needed !!!

 

public class reCAPTCHA {
    private static string secret = '6LdggcISAAAAADOONstoCow060IwYpcRXfWbbVvi';
    
    public string publicKey { 
    get
     { 
     return '6LdggcISAAAAAJe6PKGBbzrl3sBH4H6caMGexSrt';
     }
     } 
        private static string baseUrl = 'http://api-verify.recaptcha.net/verify'; 
    
    public string challenge {get; set;} { challenge = null; } 
    public string response {get; set; }  { response = null; } 
    public Boolean correctResponse { get; private set; } { correctResponse = false; } 
    
    public PageReference verify() {
      system.debug( 'verify called ');
          // first time thru, this parameter is null, so no request to make yet
    if ( challenge == null || response == null ) { 
            system.debug( 'verify called null ');
            return verify(); 
          }
                       
      HttpResponse r = makeRequest(  baseUrl ,
           'privatekey='+ secret + 
           '&remoteip=' + remoteHost + 
           '&challenge=' + challenge +
               '&response=' + response +
               '&error=incorrect-captcha-sol'
      );
        
          if ( r!= null ) {  // is null when test methods run
            correctResponse = ( r.getBody().contains('true') );
          } 
          return null;  
    }
    public PageReference reset() {
        challenge = null;
        response = null; 
        
        
        return null; 
    }   
    public static HttpResponse makeRequest(string url, string body)  {
          HttpRequest req = new HttpRequest();   
          HttpResponse response = null;
          req.setEndpoint('http://api-verify.recaptcha.net');
          req.setMethod('POST');
          req.setBody ( body);
          try {
            Http http = new Http();
            response = http.send(req);
            System.debug('response: '+ response);
            System.debug('body: '+ response.getBody());
           
          } catch( System.Exception e) {
             System.debug('ERROR: '+ e);
          }
          return response;
        }   
       
    public string remoteHost { get { string ret = '127.0.0.1';
        // also could use x-original-remote-host 
        map<string , string> hdrs = ApexPages.currentPage().getHeaders();
        if ( hdrs.get('x-original-remote-addr')!= null)
            ret =  hdrs.get('x-original-remote-addr');
        else if ( hdrs.get('X-Salesforce-SIP')!= null)
            ret =  hdrs.get('X-Salesforce-SIP');
        return ret;
    } }
        
    public static  testmethod void test_1() {
        reCaptcha re = new reCaptcha();
        string href = baseUrl;
        re.challenge = re.response = 'foo';
        string publick = re.publicKey; 
        string host = re.remoteHost; 
        re.verify();
    }
    public static  testmethod void test_2() {
        reCaptcha re = new reCaptcha();
        re.verify();
    }
    
}
HI 
I am trying to implement a captcha which is shown in one of the blog post of Salesforce. When I hit submit, the page goes to the next page rather than verifying the values entered by the user as a response....any help, sugggestions will be appreciated....
thanks