Google Recaptcha v2 With email form, gives http 500 Error
对"联系我们"使用 html 表单。这会传递姓名、电子邮件、
这是一个对我有用的答案。我真的要感谢 Galzor,因为他的回答对我帮助很大。我从 Code Geek 获得的基本代码,我在这里添加了一些东西以添加到表单中。这种格式有望消除在 Google"SITE-KEY"和"SECRET-KEY"中确切包含什么内容的混淆,因为它在将它们处理成字符串之前将它们作为变量获取。这些实际上是 40 个字符串。成功的验证码进入登录页面。
这是 HTML send-mail_form.html
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | <!DOCTYPE html> <html> <head> <script src="https://www.google.com/recaptcha/api.js" async defer> </head> <body> <!-- form goes in the body of HTML --> <form action="send-mail_form.php" method="post"> <span>Name</span> <input type="text" name="name" value="" placeholder="Your Name" required> <span>Email</span> <input type="email" name="web_email" placeholder="[email protected]" required> <span>Messgae</span> <textarea name="message" placeholder="message" required></textarea> <!-- Google v2 Recaptcha Form --> <input type="submit" name="submit" value="Send"> </form> </body> </html> |
这就是所谓的send-mail_form.php。我不会在这里显示thank_you_SO2.html。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 | <?php error_reporting(E_ALL); ini_set('display_errors', 1); $web_email;$message;$captcha; // check form is submitted if(isset($_POST['web_email']) ){ // get values $name= $_POST["name"]; $visitor_email= $_POST['web_email']; $message= $_POST['message']; //Validate first if(empty($name)||empty($visitor_email)) { $error ="Name and email are needed!"; } if(isset($_POST['g-recaptcha-response'])){ $captcha=$_POST['g-recaptcha-response']; } if(!$captcha){ echo 'Please check the the captcha form.'; exit; } $secretKey ="SECRET-KEY"; $ip = $_SERVER['REMOTE_ADDR']; // post request to server $url = 'https://www.google.com/recaptcha/api/siteverify?secret=' . urlencode($secretKey) . '&response=' . urlencode($captcha); $response = file_get_contents($url); $responseKeys = json_decode($response,true); // should return JSON with success as true if($responseKeys["success"]) { // echo 'Thanks for contacting us'; // mail then $to ="[email protected]"; $email_subject ="CG Recaptcha Form2 submission"; $email_body ="You have received a new message from".$name.".\ ". "sender's email:\ ".$visitor_email."\ ". "Here is the message:\ ".$message; //Send the email! $mail_check = mail($to,$email_subject,$email_body); if( $mail_check ){ // echo"all is well. mail sent"; header('Location: thank_you_SO2.html'); } else { echo 'You are a spammer ! Go Away'; } } } ?> |
有一些不必要的项目,顶部的错误检查可能可以删除。谷歌网站验证也将与 https://google.com/recaptcha/api/siteverify?secret=.... 一起使用吗?实际上,在测试时,如果没有 www,它似乎有时会失败,所以最好保留它。
我看到您的代码中有很多错误。试试下面的代码,看看它是否有效,它已经过测试并为我工作。它不是基于您遵循的教程,而是使用 curl 进行验证。
我认为你最大的错误是没有定义 isInfected 函数,=> 代替 -> 并且有时 file_get_contents 不能在所有服务器上工作。
HTML:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | <script src="https://www.google.com/recaptcha/api.js" async defer> <form action="" method="post"> <span>Name</span> <input type="text" name="name" placeholder="Your Name" required> <span>Email</span> <input type="email" name="web_email" placeholder="[email protected]" required> <span>Messgae</span> <textarea name="message" placeholder="message" required></textarea> <!-- Google v2 Recaptcha Form --> <input type="submit" name="submit" value="Send"> </form> |
PHP 代码:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 | <?php //check form is submitted if( isset($_POST['submit']) ){ // get values $error = ''; $name = $_POST["name"]; $visitor_email = $_POST['web_email']; $message = $_POST["message"]; //Validate first if(empty($name)||empty($visitor_email)) { $error ="Name and email are needed!"; } //handle captcha response $captcha = $_REQUEST['g-recaptcha-response']; $handle = curl_init('https://www.google.com/recaptcha/api/siteverify'); curl_setopt($handle, CURLOPT_POST, true); curl_setopt($handle, CURLOPT_POSTFIELDS,"secret=YOUR_SECRET_KEY&response=$captcha"); curl_setopt($handle, CURLOPT_RETURNTRANSFER, true); $response = curl_exec($handle); $explodedArr = explode(",",$response); $doubleExplodedArr = explode(":",$explodedArr[0]); $captchaConfirmation = end($doubleExplodedArr); print_r($doubleExplodedArr); if ( trim($captchaConfirmation) !="true" ) { $error ="<p>You are a bot! Go away!</p>"; } if( empty($error) ){ //no error // mail than $to ="[email protected]"; $email_subject ="New Form submission"; $email_body ="You have received a new message from".$name.".\ ". "sender's email:\ ".$visitor_email."\ ". "Here is the message:\ ".$message; $headers ="From:".$visitor_email." \ \ "; $headers .="Reply-To:".$visitor_email." \ \ "; //Send the email! $mail_check = mail($to,$email_subject,$email_body,$headers); if( $mail_check ){ // echo"all is well. mail sent"; header('Location: thank_you.html'); } else { echo"mail failed. try again"; } } else { echo $error; } } ?> |