php - 将 reCAPTCHA v2 添加到我的 PHP 文件中

标签 php html captcha recaptcha

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。有了这个,我也得到了我想要的发件人字段,即“发件人姓名”。

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
     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.";
            }  
    ?>

关于php - 将 reCAPTCHA v2 添加到我的 PHP 文件中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29527617/

相关文章:

javascript - 在本地提供 Google No Captcha reCaptcha 的 api.js 是否会阻止正确的行为?

c# - 验证码控件不适用于 url 路由 (ASP.NET 4.0 WebForms)

scala - 使用scala在play框架中生成验证码

php - 非持久性 pg 连接

html - IE 8 中的坏像素背景图像

php - 是否可以使用 TinyButStrong 和 OpenTBS 插件输出 .docx 和 .odt 以外的格式

php - TCPDF 返回奇怪的字符

html - 如何使两个不同的事物并排在一起?

php - mysql 查询 - 有限制的博客文章和评论

php - 在 Windows 上使用 XAMPP 安装 PHP YAML 扩展