Jump to content

Upgrading to Google reCAPTCHA v2


ben_1uk
Go to solution Solved by scootstah,

Recommended Posts

Hi everyone,

 

I'm hoping someone can help me out!

 

I am currently trying to upgrade to the latest version of Google reCAPTCHA. My site is currently using the old version. I am validating the user input on a HTML 'contact us' form using PHP (mailer.php). Depending on the data input by the user, they will see either a 'success' or 'failure' HTML page.

 

I have managed to integrate Google reCAPTCHA v2 client side (ie, on the HTML 'Contact Us' page using the form), but I'm struggling to integrate the new version when it comes to server side validation. 

 

I have included a copy of the current mailer.php file, which I somehow need to modify to upgrade to reCAPTCHA v2.

 

Any help would be really appreciated!

 

Thanks,

Ben_1uk

<?php

// check reCAPTCHA information
        require_once('recaptchalib.php');
         
        $privatekey = "my_private_key_lives_here";
        $resp = recaptcha_check_answer ($privatekey,
                                    $_SERVER["REMOTE_ADDR"],
                                    $_POST["recaptcha_challenge_field"],
                                    $_POST["recaptcha_response_field"]);
// if CAPTCHA is entered correctly

	if ($resp->is_valid) {

	$emailSubject = 'Customer Enquiry';
	$webMaster = 'my_email_address_lives_here';

// Data entered by user

	$firstname = $_POST['firstname'];
	$surname = $_POST['surname'];
	$emailaddress = $_POST['emailaddress'];
	$telephone = $_POST['telephone'];
	$enquiry = $_POST['enquiry'];

	$body = <<<EOD
<br><hr><br>
<b>First Name</b>: $firstname <br><br>
<b>Surname:</b> $surname <br><br>
<b>E-mail Address:</b> $emailaddress <br><br>
<b>Telephone No:</b> $telephone <br><br>
<b>Message:</b> $enquiry <br>
EOD;

	$mailheaders .= "From: $emailaddress\r\n";
	$headers .= "Content-type: text/html charset=iso-88590-lrn";
	$success = mail($webMaster, $emailSubject, $body, $headers);

}

else {
            // handle the CAPTCHA being entered incorrectly
            header('Location: captcha_unsuccessful.html');
        }

// Results rendered as HTML

	$theResults = <<<EOD
<html>
<head>
<link rel="icon" href="favicon.gif" type="image/gif" />
<title>Woodside Enquiry Form</title>
<meta http-equiv='Content-Type' content='text/html'>
<style type='text/css'>
<!--
body
{
margin: 0;
padding: 0;
font-size: 95%;
font-family: georgia, times, "times new roman", serif;
color: #000;
background-color: #3399CC;
}

a:link { color: #036; }
a:visited { color: #003366; }

a:hover, a:active
{
color: #fff;
background-color: #036;
}

div#pagewrap
{
width:980px;
margin: 0 auto;
background-color: #fff;
}

div#banner
{
background-image: url(banner2.jpg);
height: 250px;
width: 100%;
background-position: 0, 0;
background-repeat: no-repeat;
border-bottom: 1px solid #000;
}

div#banner h1
{
margin: 0;
padding: .3em 0 .3em .5em;
font-size: 2.2em;
font-weight: normal;
}

div#container
{
background-image: url(nav_col_base.jpg);
background-repeat: repeat-y;
}

div#container2
{
background-image: url(more_col_base.jpg);
background-repeat: repeat-y;
background-position: right;
}

div#navigation
{
float: left;
width: 150px;
padding-top: 2em;
}

div#navigation ul
{
list-style-type: none;
padding: 0;
margin: 0;
}

div#navigation ul li { margin-top: 4px; }

#navigation ul li a
{
display: block;
width: 135px;
padding: 3px 5px 3px 10px;
text-decoration: none;
color: #fff;
background-image: url(nav_base.jpg);
background-repeat: repeat-y;
}

#navigation ul li a:hover
{
color: #fff;
background-color: #ccc;
background-image: url(nav_base2.jpg);
background-repeat: repeat-y;
}

div#more
{
float: right;
width: 160px;
margin: 0;
padding: 2em 10px 0 0;
color: #fff;
}

div#more h3
{
margin-top: 0;
color: #fff;
padding: .2em;
background-image: url(more_base.jpg);
background-position: right;
background-repeat: repeat-y;
}

div#content
{
margin-left: 190px;
margin-right: 200px;
}

div#content h2
{
font-size: 2em;
color: #036;
margin: 0;
padding-top: 1em;
font-weight: normal;
}

div#content { line-height: 150%; }

#cleardiv
{
clear: both;
height: 1em;
}

div#footer
{
clear: both;
padding: .5em 1em;
border-top: 1px solid #999;
text-align: right;
}

div#footer ul
{
padding: 0;
margin: 0;
list-style-type: none;
}

div#footer li
{
display: inline;
margin-right: 1em;
} 
-->
</style>
</head>
<body>
<div id="pagewrap">

<div id="banner">
</div>

<div id="container">
	<div id="container2">
	<div id="navigation">
		<ul>
			<li><a href="index.html">Home</a></li>
			<li><a href="pools.html">Pools</a></li>
			<li><a href="rules.html">Fishery Rules</a></li>
			<li><a href="diary.html">Diary</a></li>
			<li><a href="results.html">Match Results</a></li>
			<li><a href="facilities.html">Facilities</a></li>
			<li><a href="cabins.html">Accommodation</a></li>
			<li><a href="news.html">News</a></li>
			<li><a href="contactus3.php">Contact Us</a></li>
			<li><a href="gallery.html">Gallery</a></li>
			<li><a href="links.html">Links</a></li>
			<li><a href="testimonials.php">Testimonials</a></li>
			<li><a href="wacs.html">Woodside ACS</a></li>
			<li><a href="video.html">Video</a></li>
			<li><a href="press.html">Press</a></li>
		</ul>
	</div>

<div id="more">
</div>

<div id="content">
<h2>Thank you for your submission.</h2>
<p>Your enquiry will be processed shortly.</p>
<p>Please note that if you did not submit the relevant CAPTCHA information, or entered the information requested incorrectly, your E-mail request will not reach our server and will not be processed. We are not ignoring you!</p>
<p>E-mails sent to the fishery are usually responded to the same day. If you have not received a response within 48 hours, please call 01299 270565 or 07968 111 415.</p>
<p>Thank you.</p>
<p><a href="index.html" />Return to the homepage</a>.</p>
</body>
</div>


<div id="cleardiv"></div>

</div>

</div>

<div id="footer">
		<ul>
			<li><a href="charliespool.html">Charlies Pool</a></li>
			<li><a href="housepool.html">House Pool</a></li>
			<li><a href="islandpool.html">Island Pool</a></li>
			<li><a href="woodsidepool.html">Woodside Pool</a></li>
			<li>©Woodside Fishery 2012</li>
		</ul>
</div>
</html>

EOD;
echo "$theResults";
?>
Link to comment
Share on other sites

The way you check the captcha value is different in the V2.  Here is a simplified version of a function I use to validate the captcha

function validCaptchaV2()
{	
	$secret_key = 'Your secret key';

	$resp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret_key."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
	
	$answer = json_decode($resp);
	
	if($answer->success === FALSE)
	{ 
		//Do whatever logic you want for the error here
		//Errors::set("The Captcha did not pass validation. Please try again."); 
		return FALSE;
	}
	else{ return TRUE; }
}

Link to comment
Share on other sites

Thanks for the response fastsol,

 

I have amended my existing code based on your suggestion, and I'm a little further forward with it now.

 

However, the information from the form is not posting to my Email address(?) and I seem to be able to completely by-pass the reCAPTCHA validation by simply clicking the 'submit' button(?) on the form. I have clearly still got something wrong.

 

Here is my amended code:

<?php

function validCaptchaV2()
{	
	$secret_key = 'my_private_key_goes_here';

	$resp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret_key."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
	
	$answer = json_decode($resp);
	
	if($answer->success === FALSE)
	{ 
		// // handle the CAPTCHA being entered incorrectly
            header('Location: captcha_unsuccessful.html');
 
		return FALSE;
	}
	else{ return TRUE; }
}

    $firstname = $_POST['firstname'];
    $surname = $_POST['surname'];
    $emailaddress = $_POST['emailaddress'];
    $telephone = $_POST['telephone'];
    $enquiry = $_POST['enquiry'];

    $body = "First Name: $firstname \n Surname: $surname \n Telephone Number: $telephone \nMessage: $enquiry";

// if CAPTCHA is entered correctly

	if($answer->success)
 {

	$emailSubject = 'Customer Enquiry';
	$webMaster = 'my_email_goes_here';

	$body = <<<EOD
<br><hr><br>
<b>First Name</b>: $firstname <br><br>
<b>Surname:</b> $surname <br><br>
<b>E-mail Address:</b> $emailaddress <br><br>
<b>Telephone No:</b> $telephone <br><br>
<b>Message:</b> $enquiry <br>
EOD;

	$mailheaders .= "From: $emailaddress\r\n";
	$headers .= "Content-type: text/html charset=iso-88590-lrn";
	$success = mail($webMaster, $emailSubject, $body, $headers);

}

// Results rendered as HTML

	$theResults = <<<EOD
<html>
<head>
<link rel="icon" href="favicon.gif" type="image/gif" />
<title>Woodside Enquiry Form</title>
<meta http-equiv='Content-Type' content='text/html'>
<style type='text/css'>
<!--
body
{
margin: 0;
padding: 0;
font-size: 95%;
font-family: georgia, times, "times new roman", serif;
color: #000;
background-color: #3399CC;
}

a:link { color: #036; }
a:visited { color: #003366; }

a:hover, a:active
{
color: #fff;
background-color: #036;
}

div#pagewrap
{
width:980px;
margin: 0 auto;
background-color: #fff;
}

div#banner
{
background-image: url(banner2.jpg);
height: 250px;
width: 100%;
background-position: 0, 0;
background-repeat: no-repeat;
border-bottom: 1px solid #000;
}

div#banner h1
{
margin: 0;
padding: .3em 0 .3em .5em;
font-size: 2.2em;
font-weight: normal;
}

div#container
{
background-image: url(nav_col_base.jpg);
background-repeat: repeat-y;
}

div#container2
{
background-image: url(more_col_base.jpg);
background-repeat: repeat-y;
background-position: right;
}

div#navigation
{
float: left;
width: 150px;
padding-top: 2em;
}

div#navigation ul
{
list-style-type: none;
padding: 0;
margin: 0;
}

div#navigation ul li { margin-top: 4px; }

#navigation ul li a
{
display: block;
width: 135px;
padding: 3px 5px 3px 10px;
text-decoration: none;
color: #fff;
background-image: url(nav_base.jpg);
background-repeat: repeat-y;
}

#navigation ul li a:hover
{
color: #fff;
background-color: #ccc;
background-image: url(nav_base2.jpg);
background-repeat: repeat-y;
}

div#more
{
float: right;
width: 160px;
margin: 0;
padding: 2em 10px 0 0;
color: #fff;
}

div#more h3
{
margin-top: 0;
color: #fff;
padding: .2em;
background-image: url(more_base.jpg);
background-position: right;
background-repeat: repeat-y;
}

div#content
{
margin-left: 190px;
margin-right: 200px;
}

div#content h2
{
font-size: 2em;
color: #036;
margin: 0;
padding-top: 1em;
font-weight: normal;
}

div#content { line-height: 150%; }

#cleardiv
{
clear: both;
height: 1em;
}

div#footer
{
clear: both;
padding: .5em 1em;
border-top: 1px solid #999;
text-align: right;
}

div#footer ul
{
padding: 0;
margin: 0;
list-style-type: none;
}

div#footer li
{
display: inline;
margin-right: 1em;
} 
-->
</style>
</head>
<body>
<div id="pagewrap">

<div id="banner">
</div>

<div id="container">
	<div id="container2">
	<div id="navigation">
		<ul>
			<li><a href="index.html">Home</a></li>
			<li><a href="pools.html">Pools</a></li>
			<li><a href="rules.html">Fishery Rules</a></li>
			<li><a href="diary.html">Diary</a></li>
			<li><a href="results.html">Match Results</a></li>
			<li><a href="facilities.html">Facilities</a></li>
			<li><a href="cabins.html">Accommodation</a></li>
			<li><a href="news.html">News</a></li>
			<li><a href="contactus3.php">Contact Us</a></li>
			<li><a href="gallery.html">Gallery</a></li>
			<li><a href="links.html">Links</a></li>
			<li><a href="testimonials.php">Testimonials</a></li>
			<li><a href="wacs.html">Woodside ACS</a></li>
			<li><a href="video.html">Video</a></li>
			<li><a href="press.html">Press</a></li>
		</ul>
	</div>

<div id="more">
</div>

<div id="content">
<h2>Thank you for your submission.</h2>
<p>Your enquiry will be processed shortly.</p>
<p>Please note that if you did not submit the relevant CAPTCHA information, or entered the information requested incorrectly, your E-mail request will not reach our server and will not be processed. We are not ignoring you!</p>
<p>E-mails sent to the fishery are usually responded to the same day. If you have not received a response within 48 hours, please call 01299 270565 or 07968 111 415.</p>
<p>Thank you.</p>
<p><a href="index.html" />Return to the homepage</a>.</p>
</body>
</div>


<div id="cleardiv"></div>

</div>

</div>

<div id="footer">
		<ul>
			<li><a href="charliespool.html">Charlies Pool</a></li>
			<li><a href="housepool.html">House Pool</a></li>
			<li><a href="islandpool.html">Island Pool</a></li>
			<li><a href="woodsidepool.html">Woodside Pool</a></li>
			<li>©Woodside Fishery 2012</li>
		</ul>
</div>
</html>

EOD;
echo "$theResults";
?>
Edited by ben_1uk
Link to comment
Share on other sites

You didn't call the function, you only defined it.  Based on how it looks like you want to validate the captcha, do this instead.

<?php
function validCaptchaV2()
{	
	$secret_key = 'my_private_key_goes_here';
 
	$resp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret_key."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
	
	$answer = json_decode($resp);
	
	return $answer->success;
}

$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$emailaddress = $_POST['emailaddress'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];

$body = "First Name: $firstname \n Surname: $surname \n Telephone Number: $telephone \nMessage: $enquiry";

// if CAPTCHA is entered incorrectly
if(validCaptchaV2() === FALSE)
{ header('Location: captcha_unsuccessful.html'); exit; }
else
{
   // If CAPTCHA is valid
   $emailSubject = 'Customer Enquiry';
   $webMaster = 'my_email_goes_here';

Link to comment
Share on other sites

Thanks again fastsol. I really do appreciate the help. The documentation from Google is next to useless! V1 was not as hard to implement as this!

 

I have made the above change to my code, but I am now receiving this error message:

 

Fatal error: Call to undefined function: json_decode() in /...filepath/somewhereonmycomputer/stillsomethingwrong... on line 8

 

<?php
function validCaptchaV2()
{	
	$secret_key = 'my_private_key_lives_here';
 
	$resp = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=".$secret_key."&response=".$_POST['g-recaptcha-response']."&remoteip=".$_SERVER['REMOTE_ADDR']);
	
	$answer = json_decode($resp);
	
	return $answer->success;
}

I've had this fatal error message once before with another example.

 

Regards,

Ben_1uk

Link to comment
Share on other sites

It's likely that your captcha function is not working properly due to using file_get_contents() on a URL. It is probably not returning anything. The reason is because there is a PHP setting called allow_fopen_url that must be enabled in order to use a URL in some functions, including file_get_contents(). This is a somewhat dangerous setting and is usually disabled by default.

 

Here is an alternative using CURL:

function validCaptchaV2()
{
    $secret_key = 'my_private_key_lives_here';

    $ch = curl_init(sprintf(
        'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s',
        $secret_key,
        $_POST['g-recaptcha-response'],
        $_SERVER['REMOTE_ADDR']
    ));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $resp = curl_exec($ch);
    
    curl_close($ch);

    if (($answer = json_decode($resp)) !== null) {
        return $answer->success;
    }

    return false;
}
Link to comment
Share on other sites

Thanks scootstah,

 

So, my code now looks something like this:

<?php

function validCaptchaV2()
{
    $secret_key = 'my_private_key_lives_here';

    $ch = curl_init(sprintf(
        'https://www.google.com/recaptcha/api/siteverify?secret=%s&response=%s&remoteip=%s',
        $secret_key,
        $_POST['g-recaptcha-response'],
        $_SERVER['REMOTE_ADDR']
    ));

    curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
    
    $resp = curl_exec($ch);
    
    curl_close($ch);

    if (($answer = json_decode($resp)) !== null) {
        return $answer->success;
    }

    return false;
}

$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$emailaddress = $_POST['emailaddress'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];

$body = "First Name: $firstname \n Surname: $surname \n Telephone Number: $telephone \n Message: $enquiry";

// if CAPTCHA is entered incorrectly
if(validCaptchaV2() === FALSE)
{ header('Location: captcha_unsuccessful.html'); exit; }
else
{
   // If CAPTCHA is valid
   $emailSubject = 'Customer Enquiry';
   $webMaster = 'my_email_address_lives_here';
}

?>

// Results rendered as HTML

	$theResults = <<<EOD
<html>
<head>
<link rel="icon" href="favicon.gif" type="image/gif" />
<title>Woodside Enquiry Form</title>
<meta http-equiv='Content-Type' content='text/html'>
<style type='text/css'>
<!--
body
{
margin: 0;
padding: 0;
font-size: 95%;
font-family: georgia, times, "times new roman", serif;
color: #000;
background-color: #3399CC;
}

a:link { color: #036; }
a:visited { color: #003366; }

a:hover, a:active
{
color: #fff;
background-color: #036;
}

div#pagewrap
{
width:980px;
margin: 0 auto;
background-color: #fff;
}

div#banner
{
background-image: url(banner2.jpg);
height: 250px;
width: 100%;
background-position: 0, 0;
background-repeat: no-repeat;
border-bottom: 1px solid #000;
}

div#banner h1
{
margin: 0;
padding: .3em 0 .3em .5em;
font-size: 2.2em;
font-weight: normal;
}

div#container
{
background-image: url(nav_col_base.jpg);
background-repeat: repeat-y;
}

div#container2
{
background-image: url(more_col_base.jpg);
background-repeat: repeat-y;
background-position: right;
}

div#navigation
{
float: left;
width: 150px;
padding-top: 2em;
}

div#navigation ul
{
list-style-type: none;
padding: 0;
margin: 0;
}

div#navigation ul li { margin-top: 4px; }

#navigation ul li a
{
display: block;
width: 135px;
padding: 3px 5px 3px 10px;
text-decoration: none;
color: #fff;
background-image: url(nav_base.jpg);
background-repeat: repeat-y;
}

#navigation ul li a:hover
{
color: #fff;
background-color: #ccc;
background-image: url(nav_base2.jpg);
background-repeat: repeat-y;
}

div#more
{
float: right;
width: 160px;
margin: 0;
padding: 2em 10px 0 0;
color: #fff;
}

div#more h3
{
margin-top: 0;
color: #fff;
padding: .2em;
background-image: url(more_base.jpg);
background-position: right;
background-repeat: repeat-y;
}

div#content
{
margin-left: 190px;
margin-right: 200px;
}

div#content h2
{
font-size: 2em;
color: #036;
margin: 0;
padding-top: 1em;
font-weight: normal;
}

div#content { line-height: 150%; }

#cleardiv
{
clear: both;
height: 1em;
}

div#footer
{
clear: both;
padding: .5em 1em;
border-top: 1px solid #999;
text-align: right;
}

div#footer ul
{
padding: 0;
margin: 0;
list-style-type: none;
}

div#footer li
{
display: inline;
margin-right: 1em;
} 
-->
</style>
</head>
<body>
<div id="pagewrap">

<div id="banner">
</div>

<div id="container">
	<div id="container2">
	<div id="navigation">
		<ul>
			<li><a href="index.html">Home</a></li>
			<li><a href="pools.html">Pools</a></li>
			<li><a href="rules.html">Fishery Rules</a></li>
			<li><a href="diary.html">Diary</a></li>
			<li><a href="results.html">Match Results</a></li>
			<li><a href="facilities.html">Facilities</a></li>
			<li><a href="cabins.html">Accommodation</a></li>
			<li><a href="news.html">News</a></li>
			<li><a href="contactus3.php">Contact Us</a></li>
			<li><a href="gallery.html">Gallery</a></li>
			<li><a href="links.html">Links</a></li>
			<li><a href="testimonials.php">Testimonials</a></li>
			<li><a href="wacs.html">Woodside ACS</a></li>
			<li><a href="video.html">Video</a></li>
			<li><a href="press.html">Press</a></li>
		</ul>
	</div>

<div id="more">
</div>

<div id="content">
<h2>Thank you for your submission.</h2>
<p>Your enquiry will be processed shortly.</p>
<p>Please note that if you did not submit the relevant CAPTCHA information, or entered the information requested incorrectly, your E-mail request will not reach our server and will not be processed. We are not ignoring you!</p>
<p>E-mails sent to the fishery are usually responded to the same day. If you have not received a response within 48 hours, please call 01299 270565 or 07968 111 415.</p>
<p>Thank you.</p>
<p><a href="index.html" />Return to the homepage</a>.</p>
</body>
</div>


<div id="cleardiv"></div>

</div>

</div>

<div id="footer">
		<ul>
			<li><a href="charliespool.html">Charlies Pool</a></li>
			<li><a href="housepool.html">House Pool</a></li>
			<li><a href="islandpool.html">Island Pool</a></li>
			<li><a href="woodsidepool.html">Woodside Pool</a></li>
			<li>©Woodside Fishery 2012</li>
		</ul>
</div>
</html>

EOD;
echo "$theResults";
?>
Link to comment
Share on other sites

You're using a real key here, right? $secret_key = 'my_private_key_lives_here';

 

If not, you need to register your site with Google if you haven't already, and get a real key to send in the request.

 

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

 

Also, what is now happening? Is the script stopping here?

if(validCaptchaV2() === FALSE)
{ header('Location: captcha_unsuccessful.html'); exit; }
Link to comment
Share on other sites

You're using a real key here, right? $secret_key = 'my_private_key_lives_here';

 

If not, you need to register your site with Google if you haven't already, and get a real key to send in the request.

 

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

 

Also, what is now happening? Is the script stopping here?

if(validCaptchaV2() === FALSE)
{ header('Location: captcha_unsuccessful.html'); exit; }

 

Yes, I have a private key. I'm just hiding it for use on the forum.

 

No, the script carries on after the instruction to render the unsuccessful.html page.

{
   // If CAPTCHA is valid
   $emailSubject = 'Customer Enquiry';
   $webMaster = 'my_email_address_lives_here';
}
Link to comment
Share on other sites

I think my punctuation is a little off somewhere - too many ) : and } for me!

 

I have tried moving the exit; } part of the code to include the rest of the script but I am now receiving the following error message:

 

Parse error: syntax error, unexpected 'else' (T_ELSE) in /somewhereonmycomputer/webdirectory/mailercopycopycopy.php on line 38

Link to comment
Share on other sites

You should leave the exit there, so that the script terminates immediately when the header redirect is called. Otherwise you'd just send the email anyway.

 

On that note, I'm not seeing where you're actually sending an email. You create the variables, but I see no call to mail(). Where are you doing that?

Link to comment
Share on other sites

The code is the same as when I had reCAPTCHA v1 working. I have checked the current 'live' mailer.php file and it does not include a mail() call yet still works?

 

Here is the part of the code that should send the mail:

$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$emailaddress = $_POST['emailaddress'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];

$body = "First Name: $firstname \n Surname: $surname \n Telephone Number: $telephone \n Message: $enquiry";

// if CAPTCHA is entered incorrectly
if(validCaptchaV2() === FALSE)
{ header('Location: captcha_unsuccessful.html'); exit; }
else
{
   // If CAPTCHA is valid
   $emailSubject = 'Customer Enquiry';
   $webMaster = 'my_email_address';
}

Where would I need to add the mail() function? The examples I have looked at on-line are structured differently to mine.

Link to comment
Share on other sites

The code in your original post had a call to mail().

 

 

$mailheaders .= "From: $emailaddress\r\n";
$headers .= "Content-type: text/html charset=iso-88590-lrn";
$success = mail($webMaster, $emailSubject, $body, $headers);

 

I would assume you'd want the mail() in your else block.

Link to comment
Share on other sites

I've had another look at the code and have come up with the below. However, it still doesn't post to my email address.

//collect user data from form and post

$firstname = $_POST['firstname'];
$surname = $_POST['surname'];
$emailaddress = $_POST['emailaddress'];
$telephone = $_POST['telephone'];
$enquiry = $_POST['enquiry'];

//format look of email

$body = "First Name: $firstname \n Surname: $surname \n E-mail: $emailaddress \n Telephone Number: $telephone \n Message: $enquiry";

// if CAPTCHA is entered incorrectly
if(validCaptchaV2() === FALSE)
{ header('Location: captcha_unsuccessful.html'); exit; }
else
{
   // If CAPTCHA is valid send email
   $emailSubject = 'Customer Enquiry';
   $webMaster = 'my_email_address';   
   $mailheaders .= "From: $emailaddress\r\n";
   $headers .= "Content-type: text/html charset=iso-88590-lrn";

// send email

   $success = mail($webMaster, $emailSubject, $body, $headers);   
  
}

Link to comment
Share on other sites

You've got two differently named variables here:

$mailheaders .= "From: $emailaddress\r\n";
$headers .= "Content-type: text/html charset=iso-88590-lrn";
Where are these being created? It's bad practice to concatenate a variable (the .=) without it first being initialized. Assuming it is not created further up in the code, I'm thinking you want:
$headers = "From: $emailaddress\r\n";
$headers .= "Content-type: text/html charset=iso-88590-lrn";
Also, make sure $emailaddress exists. I don't see that either.

 

Finally, what is $success equal to?

Link to comment
Share on other sites

You've got two differently named variables here:

$mailheaders .= "From: $emailaddress\r\n";
$headers .= "Content-type: text/html charset=iso-88590-lrn";

Where are these being created? It's bad practice to concatenate a variable (the .=) without it first being initialized. Assuming it is not created further up in the code, I'm thinking you want:

 

I will amend the differently named variables as you suggest below.

$headers = "From: $emailaddress\r\n";
$headers .= "Content-type: text/html charset=iso-88590-lrn";

Also, make sure $emailaddress exists. I don't see that either.

 

$emailaddress is being used from the data gathered from the HTML form isn't it..?

 

Finally, what is $success equal to?

 

I'm not really sure. I'm trying to modify my old script that worked, but the trouble is I'm struggling to get my head round it all!

 

 

I'm really sorry for all the 'bad code', but the lack of documentation from Google when upgrading from reCAPTCHA v1 to v2 is not helping! Plus, every example I look at on the Internet is different so I'm just getting more and more confused by it all!

Edited by ben_1uk
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.