So I'm trying to get ReCaptcha to work on ColdFusion (full source code below). I tried using both get/post and I get the same error.
I/O Exception: Name in certificate `google.com' does not match host name `www.google.com'
I converted my code to PHP (full source code below) and it works fine. The only difference is that the PHP code is using get method only.
Any ideas what I could be doing wrong? Thanks for the help.
Coldfusion code:
<cfif StructKeyExists(Form, "submit")>
<cfset googleurl = "https://www.google.com/recaptcha/api/siteverify" />
<cfset recaptchasecret = "secret-key-here" />
<cfset recaptcha = FORM["g-recaptcha-response"] >
<cfset remoteip = CGI["remote_addr"] />
<!---post method--->
<!---<cfhttp url="#googleurl#" method="post" resolveURL="yes">
<cfhttpparam type="formfield" name="secret" value="#recaptchasecret#" />
<cfhttpparam type="formfield" name="response" value="#recaptcha]#" />
<cfhttpparam type="formfield" name="remoteip" value="#remoteip#" />
</cfhttp>--->
<!---get method--->
<cfhttp url="#googleurl#?secret=#recaptchasecret#&response=#recaptcha#&remoteip=#remoteip#" method=get/>
<cfdump var="#cfhttp#"><hr>
<cfabort>
</cfif>
<html>
<head>
<title>Google recapcha demo - Codeforgeek</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Google reCAPTHA Demo</h1>
<form id="comment_form" action="test.cfm" method="post">
<input type="email" placeholder="Type your email" size="40"><br><br>
<textarea name="comment" rows="8" cols="39"></textarea><br><br>
<input type="submit" name="submit" value="Post comment"><br><br>
<div class="g-recaptcha" data-sitekey="site-key-here"></div>
</form>
</body>
</html>
PHP code:
<?php
if ($_SERVER['REQUEST_METHOD'] == 'POST') {
var_dump($_POST);
$googleurl = "https://www.google.com/recaptcha/api/siteverify";
$secretKey = "secret-key-here";
$captcha=$_POST['g-recaptcha-response'];
$ip = $_SERVER['REMOTE_ADDR'];
$response=file_get_contents($googleurl."?secret=".$secretKey."&response=".$captcha."&remoteip=".$ip);
$responseKeys = json_decode($response,true);
var_dump($responseKeys);
}
?>
<html>
<head>
<title>Google recapcha demo - Codeforgeek</title>
<script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
<h1>Google reCAPTHA Demo</h1>
<form id="comment_form" action="test.php" method="post">
<input type="email" placeholder="Type your email" size="40"><br><br>
<textarea name="comment" rows="8" cols="39"></textarea><br><br>
<input type="submit" name="submit" value="Post comment"><br><br>
<div class="g-recaptcha" data-sitekey="site-key-here"></div>
</form>
</body>
</html>
It's a SSL issue - java is not able to connect to "www.google.com" properly. As a temporary workaround, use <cfset googleurl = "https://google.com/recaptcha/api/siteverify" />
. But definitely update java's cert store as well.