将Recaptcha V2添加到现有的php联系人表单

时间:2018-12-31 06:15:01

标签: php

我是php的新手,希望对您有所帮助,请将recaptcha v2添加到我现有的联系表单中(该表单本身可以正常工作)。

在此处搜索答案只会使我更加困惑如何操作。

recaptcha窗口小部件显示在正确的位置,但是我不知道该行的放置位置。

if ($result['success'] == 1)

我的联系页面代码如下

    <?php 
    $page_title="someone Contact";
    $pgKeywords="someone,Contact";
    $pgDesc="someone Contact page";
    include($_SERVER['DOCUMENT_ROOT']."/header.php");
    ?>
    <script src='https://www.google.com/recaptcha/api.js'></script> 
    <div class="w3-threequarter w3-container">

        <div class="w3-container">
        <div class="w3-center">
        <h2 class="w3-text-blue" style="font-weight:bold">Contact Me</h2>

        </div>
        <p id="pre"><strong>Normally, you should receive a reply from me within 48hrs. 
        If you haven't, please check your SPAM folder first just in case my reply ended up there. 
        </strong></p>


    <?php
    $siteKey = 'Site key here';
    $secret = 'Secret key here';

    if (isset($_POST['submit'])) {
    $response = $_POST['g-recaptcha-response'];
    $remoteip = $_SERVER['REMOTE_ADDR'];

    $url = file_get_contents("https://www.google.com/recaptcha/api/siteverify?secret=$secret&response=$response&remoteip=$remoteip");
    $result = json_decode($url, TRUE);

    }
    //********* where do I put this? ***********
    if ($result['success'] == 1)
    //******************************************

    echo "<fieldset>
        <legend><h2>Contact form</h2></legend>";
        "<p class=\"note\"><br />
          Please complete all items.<br />
        </p>";

    $form_recipient = "someone@internode.on.net";
    $form_subject = "someone Contact Form";
    $this_webpage = basename($_SERVER['PHP_SELF']);

    //******************** form processor PHP code

    $someone_form = array(); // re-initialize the form session data

   if ($_SERVER['REQUEST_METHOD'] == 'POST' && @$_POST['do'] == 'contact') {
    // if the form has been submitted

    foreach($_POST as $key => $value) {
      if (!is_array($key)) {
        // sanitize the input data
        if ($key != 'someone_message') $value = strip_tags($value);
        $_POST[$key] = htmlspecialchars(stripslashes(trim($value)));
      }
    }

    $name    = @$_POST['someone_form_name'];    // name from the form
    $email   = @$_POST['someone_form_email'];   // email from the form
    $subject = @$_POST['someone_form_subject']; // subject of the form
    $message = @$_POST['someone_form_message']; // the message from the form

    $name    = substr($name, 0, 64);  // limit name to 64 characters

    $errors = array();  // initialize empty error array

      if (strlen($name) < 3) {
        // name too short, add error
        $errors['name_error'] = 'Your name is required.';
      }

      if (strlen($email) == 0) {
        // no email address given
        $errors['email_error'] = 'Email address is required.';
      } else if ( !preg_match('/^(?:[\w\d]+\.?)+@(?:(?:[\w\d]\-?)+\.)+\w{2,4}$/i', $email)) {
        // invalid email format
        $errors['email_error'] = 'Email address entered is invalid';
      }

      if (strlen($subject) < 3) {
        // subject too short, add error
        $errors['subject_error'] = 'Please enter a subject.';
      }

      if (strlen($message) < 20) {
        // message length too short
        $errors['message_error'] = 'Please enter a message. We were expecting a few more words!';
      }

    if (sizeof($errors) == 0) {

    // no errors, send the form 

    //      $time       = date('r');
      date_default_timezone_set('Australia/Adelaide'); //sets timezone for date/time stamp
      $time = date("j F Y, g:i a") . " CDST (Australia)";

      $message = "The following message was submitted using the someone contact form.<br /><br />"
                    . "Name: $name<br />"
                    . "Email: $email<br />"
                    . "Subject: $subject<br />"
                    . "Message:<br />"
                    . "<pre>$message</pre>"
                    . "<br /><br />IP Address: {$_SERVER['REMOTE_ADDR']}<br />"
                    . "Date/time: $time<br />"
                    . "Browser: {$_SERVER['HTTP_USER_AGENT']}<br />";

      $message = wordwrap($message, 70);

        mail($form_recipient, $form_subject, $message, "From: {$form_recipient}\r\nReply-To: {$email}\r\nContent-type: text/html; charset=ISO-8859-1\r\nMIME-Version: 1.0");

      $someone_form['error'] = false;  // no error with form
      $someone_form['success'] = true; // message sent

    } else {
      // save the entries to re-populate the form
      $someone_form['someone_form_name'] = $name;       // save name from the form submission
      $someone_form['someone_form_email'] = $email;     // save email
      $someone_form['someone_form_subject'] = $subject; // save subject
      $someone_form['someone_form_message'] = $message; // save message

      foreach($errors as $key => $error) {
        // set up error messages to display with each field
        $someone_form[$key] = "<span style=\"font-weight: bold; color: #f00\">$error</span>";
      }

      $someone_form['error'] = true; // set error flag
    }
    } // POST

    //*************** process contact form

    if (isset($someone_form['error']) &&  $someone_form['error'] == true) {    // The last form submission had one or more errors 

    echo "<span class=\"error\">There was a problem with your submission. Please correct items marked in red.</span><br /><br />";

    }

    elseif (isset($someone_form['success']) && $someone_form['success'] == true) { // form was processed successfully 

    echo "<span class=\"success\">Thankyou, your message has been sent!</span><br /><br />";

    }

    $someone_form['success'] = false; // clear success value after running


    echo "<form method=\"post\" action=\"".$_SERVER['REQUEST_URI'] . $_SERVER['QUERY_STRING']."\" id=\"contact_form\">
  <input type=\"hidden\" name=\"do\" value=\"contact\" />

    <p>
    <strong>Name:</strong>&nbsp; &nbsp;".@$someone_form['name_error']."<br />
    <input type=\"text\" name=\"someone_form_name\" size=\"35\" value=\"".htmlspecialchars(@$someone_form['someone_form_name'])."\" />
    </p>

    <p>
    <strong>Email:</strong>&nbsp; &nbsp;".@$someone_form['email_error']."<br />
    <input type=\"text\" name=\"someone_form_email\" size=\"35\" value=\"".htmlspecialchars(@$someone_form['someone_form_email'])."\" />
    </p>

    <p>
    <strong>Subject:</strong>&nbsp; &nbsp;".@$someone_form['subject_error']."<br />
    <input type=\"text\" name=\"someone_form_subject\" size=\"35\" value=\"".htmlspecialchars(@$someone_form['someone_form_subject'])."\" />
    </p>

    <p>
    <strong>Message:</strong>&nbsp; &nbsp;".@$someone_form['message_error']."<br />
    <textarea name=\"someone_form_message\" style=\"width: 450px; height: 200px\">".htmlspecialchars(@$someone_form['someone_form_message'])."</textarea>
  </p>";
    ?>      
        <div class="g-recaptcha" data-sitekey="<?php echo $siteKey;  ?>"></div>

    </div>
    <?php       
    echo  "<p>
    <br />
    <input class=\"formbutton\" type=\"submit\" value=\"Submit Message\">
    </p>

    </fieldset>";


    ?>

    </div>

0 个答案:

没有答案