簡體   English   中英

Google ReCaptcha V2 不適用於舊的 PHP 代碼

[英]Google ReCaptcha V2 not working with old PHP Code

我有一個用於組織的小型網站,最近我們的聯系頁面上充斥着垃圾郵件 email。 我希望添加 Googl ReCapture,但在驗證和提交表單時遇到了一些問題。

因此網站會檢查字段是否為空,然后允許用戶發送消息。 但現在它不會錯誤檢查字段,也不會提交或發送 email。 此外,當您訪問聯系我們頁面時,它會顯示消息“感謝您聯系...英國,我們會盡快回復您。” 甚至在輸入或單擊提交之前。

<?php
include ('includes/config.php');
$error = array();
$name = '';
$email = '';
$telephone = '';
$message = '';

//I have blanked my sitekey and secret key out
$siteKey = 'MY_SITE_KEY';
$secretKey = 'MY_SECRET_KEY';

if (isset($_POST['Send']))
{
    // Assign form data
    $name = $_POST['Name'];
    $email = $_POST['Email'];
    $telephone = $_POST['Telephone'];
    $message = $_POST['Message'];

    // Validate reCAPTCHA box 
        if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])){ 
            // Google reCAPTCHA API secret key 
            $secretKey = 'Your_reCAPTCHA_Secret_Key'; 

            // Verify the reCAPTCHA response 
            $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secretKey.'&response='.$_POST['g-recaptcha-response']);  
            // Decode json data 
            $responseData = json_decode($verifyResponse); 

            // If reCAPTCHA response is valid 
            if($responseData->success){ 

    // Check for errors
    if (empty($name)) { $error[] = 'Name'; }
    if (empty($email) || !filter_var($email, FILTER_VALIDATE_EMAIL)) { $error[] = 'Email'; }
    if (empty($telephone)) { $error[] = 'Telephone'; }
    if (empty($message)) { $error[] = 'Message'; }

    // If no errors
    if (sizeof($error) < 1) {
        // Build message
        $message = "Name: $name \n Email: $email \n Telephone: $telephone \n" . $message;

        // Send email
        mail('info@MYEMAIL.co.uk', "Message From $name", $message);

        // Reset form
        $name = '';
        $email = '';
        $telephone = '';
        $message = '';
    }
}}}
?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>

<script src="https://www.google.com/recaptcha/api.js" async defer></script>

</head>
<body>
<?php include ('includes/header.php'); ?>
<div id="wrapper">
    <div id="main">
        <h1>Contact Us</h1>
        <?php
        if (sizeof($error) > 0)
        {
            echo '<p>There were errors for the following fields:</p><ul>';
            foreach ($error as $val) {
                echo "<li>$val</li>";
            }
            echo '</ul>';
        }
        else
        {
            echo '<p>Thank you for contacting ... UK, we will get back to you shortly.</p>';
        }
        ?>
        <table cellpadding="5" cellspacing="0">
        <form method="post" action="">
            <tr>
                <td width="150"><label for="Name">Name: </label></td>
                <td><input type="text" name="Name" value="<?=$name?>" /></td>
            </tr>
            <tr>
                <td><label for="Email">Email: </label></td>
                <td><input type="text" name="Email" value="<?=$email?>" /></td>
            </tr>
            <tr>
                <td><label for="Telephone">Telephone: </label></td>
                <td><input type="text" name="Telephone" value="<?=$telephone?>" /></td>
            </tr>
            <tr>
                <td valign="top"><label for="Message">Message: </label></td>
                <td><textarea name="Message" rows="10" cols="55"><?=$message?></textarea></td>
            </tr>
            <tr>
                <div class="g-recaptcha" data-sitekey="<?php echo $siteKey; ?>"></div>
            </tr>
            <tr>
                <td></td>
                <td><button type="submit" name="Send">Send Message</button></td>
            </tr>
        </form>
        </table>
    </div>
<?php include ('includes/footer.php'); ?>
</div>
</body>
</html>

我需要它做的是檢查字段是否正確並且 ReCapture 已經過驗證,並且當用戶單擊提交時,會發送 email。

我就是這樣做的,以一種工作的方式。 首先,我的頁面顯示了recaptcha。 我沒有任何表單字段,因為此應用程序不需要它們,但您可以輕松添加它們:

<?php
$mailid = $_GET['mailid']; // which email address is needed?
?>

<html>
  <head>
    <title>My test contact verification</title>
     <script src="https://www.google.com/recaptcha/api.js" async defer></script>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/3.3.1/jquery.min.js" crossorigin="anonymous"></script>
  </head>
  <body>
  <h1>Please confirm you are not a robot</h1>
    <form action="?" method="POST">
      <input type="hidden" name="mailid" id="mailid" value="<?php echo $mailid;?>">

      <div class="g-recaptcha" data-sitekey="my-site-key" data-callback="cbtest"></div>
      <br/>
    </form>
    <br />
    <br />
    <div id="result"></div>

    <script type="text/javascript">
    function cbtest(response) {
    // now, send the response to be verified
    var dispdiv = document.getElementById("result");
    dispdiv.innerHTML = "<p>Retrieving the contact details</p>";
    var contact = document.getElementById("mailid");
    var contactsel = contact.value;
    $.ajax({
    url: "http://www.example.com/recaptcha_verify.php",
    type: "POST",
    data: {source: contactsel, response: response},
      }).done(function(data, status, xhr) {
      var ver = data.substring(0,2);
      if (ver == "no") {
      dispdiv.innerHTML = "<p>Sorry, there was a verification problem.</p>";
      } else if (ver == "OK") { 
      var out = data.substring(3);
      dispdiv.innerHTML = "<p>The contact details you requested are:<br />" + out + "</p>";
      } // end of verification response check
      })
      };

    </script>
  </body>
</html>

和我的驗證碼

<?php
if (isset($_POST['response'])) { 
$response = $_POST['response']; }
else {
        echo "no1";
    exit;
}

if (isset($_POST['source'])) {
$source = $_POST['source']; }
else {
    echo "no2";
    exit;
}

$url = "https://www.google.com/recaptcha/api/siteverify";

$secret = "my-secret";

$emails = array(
   "hidden email 1",
   "hidden email 2",
   "hidden email 3");

// is the source reference a valid one?

if ($source < 0 || $source > count($emails)) {
echo "no";
exit;
}

// create the output

 $url = $url."?secret=".$secret. "&response=".$response;

        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, TRUE);
        curl_setopt($curl, CURLOPT_TIMEOUT, 15);
        curl_setopt($curl, CURLOPT_SSL_VERIFYPEER, TRUE);
        curl_setopt($curl, CURLOPT_SSL_VERIFYHOST, TRUE); 
        $curlData = curl_exec($curl);

        curl_close($curl);

$out = $emails[$source]; 

// send the response to the verifier

   $captcha_success = json_decode($curlData, TRUE);


   if ($captcha_success['success'] == false) {
       echo "noF";
   }
   else if ($captcha_success['success'] == true) {
       echo "OK|" . $out;
   }

?>

(我已經從上面刪除了我認為是虛假代碼的內容,希望我沒有采取太多措施)。

我向用戶展示了一個只有文本和一個recaptcha 的屏幕,傳入的變量被傳遞以告訴我的代碼需要哪個隱藏的email 地址,顯然您將添加表單字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM