Forum Moderators: coopster

Message Too Old, No Replies

please check my recaptcha 3 code

recaptcha php code not working

         

scoladdy

6:16 pm on Dec 9, 2018 (gmt 0)

5+ Year Member



The following is my php file for a contact form using recaptcha 3. It doesn't work.

Submitting a form, whether recaptcha is checked or unchecked, loads the url for this page. As you see, it is supposed to load a "thank you" page. Also, the submitted form is not sent to the email address.

Have run it through PHP verifiers with no errors. And though I am obviously not a PHP programmer I do understand the logic and I have gone over and over this and simply cannot figure out what is wrong. When I remove the recaptcha code the form runs perfectly.

Assistance is appreciated!


--- contact-form.php ---

<?php

// deal with recaptcha

if(isset($_POST['g-recaptcha-response'])){
//your site secret key
$secret = 'MY SECRET KEY GOES HERE';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);

if(isset($responseData->success) && $responseData->success === true){


// set email recipient
$myemail= 'info@EXAMPLE.com';

// check all form inputs using check_input function
$name = check_input($_POST['name'], 'Please enter your name');
$email = check_input($_POST['email'], 'Please enter your email address');
$comment= check_input($_POST['comment'], 'Please add a note');

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}


// prepare message for email delivery
$message = "Hello!

Your contact form has been submitted by:

Name: $name
Email: $email
Phone: $phone

Additional Comments from $name: $comment

End of message
";





/* redirect visitor to thank you page */
header('Location: /thankyou.html');
exit();

}

else {


/* functions used for cleaning and handling errors */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css_reset.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="mq-980-1199.css" media="(min-width:980px)">
<link rel="stylesheet" href="mq-1200-plus.css" media="(min-width:1200px)">
</head>
</head>
<body>
<p class="paragraph">Please correct the following errors</p>
<p class="paragraph"><?php echo $myError; ?></p>
</body>
</html>
<?php
exit();
}
}
}
?>

lucy24

9:54 pm on Dec 9, 2018 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



/* redirect visitor to thank you page */
header('Location: /thankyou.html');
It's hard to tell from what's posted, but on a casual eyeballing it looks as if this header is being sent out while the user is already on the page--meaing that it isn't going to be sent out at all.

Also, the submitted form is not sent to the email address.
Where in the code is it supposed to be sent? I can't find a "mail" command at all (admittedly not an easy search when this whole page is littered with "mail" and "email" text strings).

What address is it supposed to go to? Is this your own server, or shared hosting? I ask because my host will only send email if it is addressed to the same domain as the one that “owns” the contact form; you can't shortcut by having it go directly to your everyday address.

scoladdy

10:50 am on Dec 10, 2018 (gmt 0)

5+ Year Member



This was omitted from OP:


 /* Send the message using mail() function */
mail($myemail, $subject, $message);



... it looks as if this header is being sent out while the user is already on the page--meaing that it isn't going to be sent out at all.


I suspect this might be the problem. This only occurs when the recaptcha code is active. Do I need to place the mail() function outside of the entire recaptcha code?

scoladdy

11:04 am on Dec 10, 2018 (gmt 0)

5+ Year Member



Without recaptcha, the "thankyou.html" redirect only works when preceded with
/
. Otherwise, submitted form loads a 404 page, because the server is looking for the thankyou page in the php file.

scoladdy

11:29 am on Dec 10, 2018 (gmt 0)

5+ Year Member



Logically, the codes seems to be saying this:

1. check recaptcha info. if success

2. receive info from form fields. if without error

3. send info to email address and load thank you page.

otherwise (if 1 or 2 not successful)

4. load error page


I notice that recaptcha doesn't come with a built-in, default, error-handling mechanism. So that needs to be written in. But the send mechanism seems to be blocked by the logic of the recaptcha.

The email address is the same as the domain. It operates perfectly when the recaptcha code is removed.


// deal with recaptcha

if(isset($_POST['g-recaptcha-response'])){
//your site secret key
$secret = 'MY SECRET KEY GOES HERE';
//get verify response data
$verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
$responseData = json_decode($verifyResponse);

if(isset($responseData->success) && $responseData->success === true){


// set email recipient
$myemail= 'info@EXAMPLE.com';

// check all form inputs using check_input function
$name = check_input($_POST['name'], 'Please enter your name');
$email = check_input($_POST['email'], 'Please enter your email address');
$comment= check_input($_POST['comment'], 'Please add a note');

/* If e-mail is not valid show error message */
if (!preg_match("/([\w\-]+\@[\w\-]+\.[\w\-]+)/", $email))
{
show_error("E-mail address not valid");
}


// prepare message for email delivery
$message = "Hello!

Your contact form has been submitted by:

Name: $name
Email: $email
Phone: $phone

Additional Comments from $name: $comment

End of message
";


/* Send the message using mail() function */
mail($myemail, $subject, $message);


/* redirect visitor to thank you page */
header('Location: /thankyou.html');
exit();

}

else {


/* functions used for cleaning and handling errors */
function check_input($data, $problem='')
{
$data = trim($data);
$data = stripslashes($data);
$data = htmlspecialchars($data);
if ($problem && strlen($data) == 0)
{
show_error($problem);
}
return $data;
}

function show_error($myError)
{
?>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<meta http-equiv="X-UA-Compatible" content="ie=edge">
<link rel="stylesheet" href="css_reset.css">
<link rel="stylesheet" href="main.css">
<link rel="stylesheet" href="mq-980-1199.css" media="(min-width:980px)">
<link rel="stylesheet" href="mq-1200-plus.css" media="(min-width:1200px)">
</head>
</head>
<body>
<p class="paragraph">Please correct the following errors</p>
<p class="paragraph"><?php echo $myError; ?></p>
</body>
</html>
<?php
exit();
}
}
}
?>