【问题标题】:Adding reCAPTCHA v2 into my PHP file将 reCAPTCHA v2 添加到我的 PHP 文件中
【发布时间】:2015-06-14 04:14:15
【问题描述】:

PHP 问题已解决。以下是我正在使用的 HTML 和 PHP 代码:

contact_form.html:

<html>
<head>
    <title>My Contact Form</title>
    <script src='https://www.google.com/recaptcha/api.js'></script>
</head>
<body>
    <div class="contact-form">
        <h3>Send me your message</h3>
        <form action="app/contact.php" method="POST">
            <div class="text-fields">
                <span>Full Name *:</span>
                <input name="full_name" type="text" class="text" value="Your Name">
            </div>
            <div class="text-fields">
                <span>E-mail *:</span>
                <input name="email" type="text" class="text" value="user@domain.com">
            </div>
            <div class="clearfix"> </div>
            </div>
                <div class="subject-field">
                <span>Subject *:</span>
                <input name="subject" type="text" class="text" value="Your Subject">
            </div>
            <div class="message-field">
                <span>Message *:</span>
                <textarea name="message"> </textarea>
            </div>
            <div class="g-recaptcha" data-sitekey="SITE-KEY-HERE"></div>
            <input type="submit" value="Send" />
        </form>
    </div>
</body>
</html>

这是更新后的 PHP。有了这个,我也得到了我想要的 From 字段,即“Sender Name”。

contact_form.php:

<?php
    $full_name;$email;$subject;$message;$captcha;
        if(isset($_POST['full_name'])){
            $full_name=$_POST['full_name'];
        }if(isset($_POST['email'])){
            $email=$_POST['email'];
        }if(isset($_POST['subject'])){
            $subject=$_POST['subject'];
        }if(isset($_POST['message'])){
            $message=$_POST['message'];
        }if(isset($_POST['g-recaptcha-response'])){
            $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
            echo 'Check the reCAPTCHA box.';
            exit;
        }
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRET-KEY-HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
        if($response.success==false)
        {
            echo 'You are a robot!';
        }else
        {
            $to = "me@domain.com";
            $from = $full_name . ' <'.$email.'>';
            $headers = 'From: ' . $from . "\r\n";
            mail ($to, $subject, $message, $headers);

        echo 'Your message has been sent!';
        }
?>

感谢大家的帮助。

【问题讨论】:

标签: php html captcha recaptcha


【解决方案1】:

试试这个!

<?php
     if(isset($_POST['full_name']) && isset($_POST['full_name']) ){
        $full_name = $_POST['full_name']; 
         $email = $_POST['email'];
         $from_name_email = $full_name . '<'.$email.'>';
     }
     if(isset($_POST['subject'])){
        $subject = $_POST['subject'];
     }
    if(isset($_POST['message'])){
        $message = $_POST['message'];
    }
     }if(isset($_POST['g-recaptcha-response'])){
        $captcha=$_POST['g-recaptcha-response'];
        $to = "my@email.com";
        $subject = "Contact Form";
        $headers .= 'From: ' . $from_name_email . "\r\n";
    }
    if(!$captcha){
        echo 'Please check the the captcha form.';
        exit;
    }
    $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=YOUR SECRET KEY GOES HERE&response=".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
            if($response.success==false)
            {
              echo 'You are spammer!';
            }else
            {
        mail ($to, $subject, $message, $headers);

        echo "Your message has been sent! Thank you.";
            }  
    ?>

【讨论】:

  • 这段代码仍然包含无意义的片段$response.success==false。这没有任何意义。
  • 如果 captha 不能正常工作,你就是垃圾邮件发送者??
  • 不,实际上不会。 $response.success 将字符串“成功”附加到$response;该操作的结果永远不会是错误的。
  • 它会如何正常工作?非常感谢,对我的错误深表歉意
  • $response['success'] 如果解析为 json 可能会起作用。为什么没有回答?
【解决方案2】:

试试这个,它对我有用:

<?php
 if(isset($_POST['full_name']) && isset($_POST['full_name']) ){
    $full_name = $_POST['full_name']; 
     $email = $_POST['email'];
     $from_name_email = $full_name . '<'.$email.'>';
 }
 if(isset($_POST['subject'])){
    $subject = $_POST['subject'];
 }
if(isset($_POST['message'])){
    $message = $_POST['message'];
}
 }if(isset($_POST['g-recaptcha-response'])){
    $captcha=$_POST['g-recaptcha-response'];
    $to = "my@email.com";
    $subject = "Contact Form";
    $headers .= 'From: ' . $from_name_email . "\r\n";
}
if(!$captcha){
    echo 'Please check the the captcha form.';
    exit;
}
//change is here
$response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=USE PRIVATE KEY HERE&response=".$_POST['g-recaptcha-response'."&remoteip=".$_SERVER['REMOTE_ADDR']);
        //change is here
        $response = json_decode($response);
        //change is here
        if($response->success==false)
        {
          echo 'You are spammer!';
        }else
        {
    mail ($to, $subject, $message, $headers);

    echo "Your message has been sent! Thank you.";
        }  
?>

【讨论】:

    【解决方案3】:

    试试:

     if(!isset($_POST['g-recaptcha-response']) || empty($_POST['g-recaptcha-response'])) {
            echo 'reCAPTHCA verification failed, please try again.';
        } else {
            $secret = 'google_secret_key';
    
            $ch = curl_init();
            curl_setopt($ch, CURLOPT_URL, 'https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
            curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
            $response = curl_exec($ch);
            curl_close($ch);
            $response = json_decode($response);
    
            if($response->success) {
                // What happens when the CAPTCHA was entered incorrectly
                echo 'Successful .';
            } else {
                // Your code here to handle a successful verification
                echo 'reCAPTHCA verification failed, please try again.';
            }
        }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-12-03
      • 1970-01-01
      • 2018-10-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多