首页 文章

自定义联系表单与谷歌reCaptcha wordpress

提问于
浏览
0

尝试使用谷歌reCaptcha实现自定义编码的联系表单,但似乎没有采取任何措施 . 这是使用wordpress(不希望使用插件'验证回来与“感谢发布评论 . ”从表单中获取没有电子邮件/消息,检查我的垃圾邮件等任何帮助将不胜感激 . 我的HTML表单在下面:

<div class="contact_form">  
        <h1>Contact Form</h1>
        <form id="comment_form" action="" method="post">
          <p>Your Name (required) 
<input type="text" name="cf-name" placeholder="type your name" pattern="[a-zA-Z0-9 ]+" size="40" /> </p> <p>Your Email (required)
<input type ="email" name="cf-email" placeholder="type your email" size ="40"><br> </p> <p>Subject (required)
<input type ="text" name="cf-subject" placeholder="type your subject" size ="40"><br> </p> <p>Your Message
<textarea rows="10" cols="35" name="cf-message"></textarea> </p> <div class="g-recaptcha" data-sitekey="SITEKEYHERE"></div> <input type="submit" name="submit" value="Post Comment"> </form> </div>

我的php发送电子邮件并检查reCaptcha:

<?php

    // if the submit button is clicked, send the email
    if ( isset( $_POST['cf-submitted'] ) ) {

        // sanitize form values
        $name    = sanitize_text_field( $_POST["cf-name"] );
        $email   = sanitize_email( $_POST["cf-email"] );
        $subject = sanitize_text_field( $_POST["cf-subject"] );
        $message = esc_textarea( $_POST["cf-message"] );

        // get the blog administrator's email address
        $to = "my@email.here";

        $headers = "From: $name <$email>" . "\r\n";

        // If email has been process for sending, display a success message
        if ( wp_mail( $to, $subject, $message, $headers ) ) {
            echo '<div>';
            echo '<p>Thanks for contacting me, expect a response soon.</p>';
            echo "$to";
            echo '</div>';
        } else {
            echo 'An unexpected error occurred';
          }
    }?>


    <?php

        $name;$email;$subject;$message;$captcha;
        if(isset($_POST['cf-name'])){
          $email=$_POST['cf-name'];
        }if(isset($_POST['cf-email'])){
          $email=$_POST['cf-email'];
        }if(isset($_POST['cf-subject'])){
          $email=$_POST['cf-subject'];
        }if(isset($_POST['cf-message'])){
          $email=$_POST['cf-message'];
        }if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRETKEYHERE".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
        if($response.success==false)
        {
          echo '<h2>spam</h2>';
        }else
        {
          echo '<h2>Thanks for posting comment.</h2>';
        }
?>

1 回答

  • 0

    在验证验证码之前,您的邮件程序代码已被触发 .

    尝试移动代码:

    <?php
    
        $name;$email;$subject;$message;$captcha;
        if(isset($_POST['cf-name'])){
          $email=$_POST['cf-name'];
        }if(isset($_POST['cf-email'])){
          $email=$_POST['cf-email'];
        }if(isset($_POST['cf-subject'])){
          $email=$_POST['cf-subject'];
        }if(isset($_POST['cf-message'])){
          $email=$_POST['cf-message'];
        }if(isset($_POST['g-recaptcha-response'])){
          $captcha=$_POST['g-recaptcha-response'];
        }
        if(!$captcha){
          echo '<h2>Please check the the captcha form.</h2>';
          exit;
        }
        $response=file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=SECRETKEYHERE".$captcha."&remoteip=".$_SERVER['REMOTE_ADDR']);
        if($response.success==false){
          echo '<h2>spam</h2>';
        }else{
            // sanitize form values
            $name    = sanitize_text_field( $_POST["cf-name"] );
            $email   = sanitize_email( $_POST["cf-email"] );
            $subject = sanitize_text_field( $_POST["cf-subject"] );
            $message = esc_textarea( $_POST["cf-message"] );
    
            // get the blog administrator's email address
            $to = "my@email.here";
    
            $headers = "From: $name <$email>" . "\r\n";
    
            // If email has been process for sending, display a success message
            if ( wp_mail( $to, $subject, $message, $headers ) ) {
                echo '<div>';
                echo '<p>Thanks for contacting me, expect a response soon.</p>';
                echo "$to";
                echo '</div>';
            } else {
                echo 'An unexpected error occurred';
           }          
    
        }
    ?>
    

    我正在手机上点击这个,这样你就可以整理这些代码了 . 但逻辑应该是正确的 .

相关问题