The solution to your problem will require the following :
- Add a google recaptcha to your main form.
- Add a file in the middle between the form and the thank you page that redirects to the thank you page.
The steps are the following :
Sign up for google recaptcha v2 service API from here :
https://www.google.com/recaptcha/about/
In the page file that has the form insert this script tag by google before the end of the </head>
html tag.
<script src="https://www.google.com/recaptcha/api.js" async defer></script>
Find the file that has the form which is most likely html, and insert this code right under the beginning of the form table, which should starts with <form bla bla... >
and replace the sitekey with the one that google gives you when you finish setting up your captaha v2
<div class="g-recaptcha" data-sitekey="xxxxxxxxxxxx"></div>
At the end of the form file page insert the following code :
<?php
// here you must input the secret key, not the site key
// don´t worry, it is server side protected and won´t be
// visible under the page source, it´s php code from now on...
$secret = "yyyyyyyyyyyyy";
$post = [
'secret' => $secret,
'response' => $_POST['g-recaptcha-response'],
'remoteip' => $_SERVER['REMOTE_ADDR']
];
$ch = curl_init('https://www.google.com/recaptcha/api/siteverify');
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
$response = curl_exec($ch);
curl_close($ch);
$array = json_decode($response,true);
if($array['success'] == 1){
echo "<script> location.href='new_url'; </script>";
exit;
echo "success!" ;
}
else{
echo "form not accepted so far....";
}
?>
- Replace $secret = "yyyyyyyyyyyyy"; with the secret code you get from google and replace new_url with the url of a file called redirect.html and create that file in your host directory.
- In the redirect.html insert this code :
<script> location.href='new_url'; </script>
- Replace the new_url of the last file with the url of the thank you page.
Above is the primary solution, but if you want to go more securely you can do something with PHP called session, which will not allow the redirect file to redirect to the thank you page unless, the user came right from the form page. To do that you will have to add this :
under that same form file add this extra php :
<?php
session_start();
?>
<?php
$_SESSION["redirectscript"] = "<script> location.href='new_url'; </script>";
?>
This time you will have to change redirect.html
to redirect.php
Change the new_url to the redirect.php
link.
Inside of redirect.php
delete everything and insert only :
<?php session_start(); ?>
<?php echo $_SESSION["redirectscript"]; ?>