对”联系我们”使用 html 表单。这会传递姓名、电子邮件、



相关讨论

  • 教程:kaplankomputing.com/blog/tutorials/recaptcha-php-demo-tutorial??al
  • 谢谢,我按照您的示例概述的行编辑了我的 HTML 和 PHP 代码。虽然还是不行,但感觉离我更近了。我看到了如何将 recaptcha 表单数据与请求姓名、电子邮件和消息的原始表单数据结合起来。它还显示了在何处添加 Google 密钥的”秘密响应”并进行比较。但是,我现在收到 http 500 错误。请注意,我将 kaplankomputing 代码中的所有引用从 http 更改为 https。
  • 哦,作为参考,您可以在此处查看工作的普通表单和非工作的 recaptcha 表单:coinsandhistory.com/#contact


这是一个对我有用的答案。我真的要感谢 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 ;
  }
}
?>



相关讨论

  • 非常感谢!但是,我遇到了一个新错误。这是 Array ( [0] => { “success” [1] => false ) 你是个机器人!离开!
  • 我应该提一下,我仍然只是在学习 javascript,还没有掌握函数。在 php 文件中,如果我将 if (trim) 测试更改为: if ( trim($captchaConfirmation) != “false” ) { $error = “<p>你是机器人!走开!<pb >”;那么无论是否选中该框,Recaptcha 总是会成功。我确实回到了谷歌,并确保我直接从谷歌复制了数据站点密钥和秘密。它们在原始形式中看起来是正确的。
  • 如果你得到 Array ( [0] => { “success” [1] => false ),这意味着谷歌没有把你当作人类传递。我添加了 print_r($doubleExplodedArr);行来显示来自 recaptcha 本身的原始响应。我认为代码中没有错误。也许尝试在同一页面上提交,而不是将表单 action=” 留空,并在同一页面上编写 php 代码。
  • 如果您对此感到非常沮丧,请随时通过 [email protected] 给我发送电子邮件。
  • 我已经添加了更多的打印语句并得到了: Array ( [0] => { “success” [1] => false ) { “success”: false, “error-codes”: [ “missing-input-response”, “missing-input-secret” ] } falsefalse 你是个机器人!离开! ……..以前即使没有recaptcha,我在发送邮件时也遇到了很多问题。我终于发现这是由于我的 ISP 在没有告诉我的情况下禁用了 send_mail()。我怀疑还有服务器问题?似乎 Google 甚至没有获得任何站点密钥代码”缺少输入密码””缺少站点密钥”。我会查看我的服务器日志。
  • 很好,看起来你正朝着正确的方向前进。我再次测试了整个代码,我为你提供了另一个 recaptcha 密钥,它运行良好。所以肯定是这个代码问题中的其他一些问题阻止了它工作。
  • 我可能发现了一个问题。在您的行中: curl_setopt($handle, CURLOPT_POSTFIELDS, “secret=YOUR_SECRET_KEY


声明:本站(华域联盟www.cnhackhy.com)所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。