尝试将Recaptcha v2添加到oscommerce联系我们页面

时间:2019-05-25 11:39:33

标签: php recaptcha oscommerce

我正在使用oscommerce,并尝试将googles repcatcha V2添加到“与我们联系”页面。该表格当前允许用户输入姓名,电子邮件地址和查询输入。用户输入此内容后,“ google recaptcha”小部件将位于“继续”按钮旁边。在用户单击继续之前,我正在努力捕捉响应。当前,用户可以单击“继续”,而小部件将被忽略。

我尝试将以下代码添加到当前的oscommerce联系我们页面:

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

        // 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($responseData->success){ 
        }

  }

我不确定在当前oscommerce代码中将if语句放入responsedata-> success的最佳位置,以及是否确实设置了$ responsedata。我正在使用崇高的文字和新的文字。

/*
  $Id$

  osCommerce, Open Source E-Commerce Solutions
  http://www.oscommerce.com

  Copyright (c) 2010 osCommerce

  Released under the GNU General Public License
*/

  require('includes/application_top.php');

  require(DIR_WS_LANGUAGES . $language . '/' . FILENAME_CONTACT_US);

  if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'send') && isset($HTTP_POST_VARS['formid']) && ($HTTP_POST_VARS['formid'] == $sessiontoken)) {
    $error = false;


    $name = tep_db_prepare_input($HTTP_POST_VARS['name']);
    $email_address = tep_db_prepare_input($HTTP_POST_VARS['email']);
    $enquiry = tep_db_prepare_input($HTTP_POST_VARS['enquiry']);

    if (!tep_validate_email($email_address)) {
      $error = true;

      $messageStack->add('contact', ENTRY_EMAIL_ADDRESS_CHECK_ERROR);
    }

    $actionRecorder = new actionRecorder('ar_contact_us', (tep_session_is_registered('customer_id') ? $customer_id : null), $name);
    if (!$actionRecorder->canPerform()) {
      $error = true;

      $actionRecorder->record(false);

      $messageStack->add('contact', sprintf(ERROR_ACTION_RECORDER, (defined('MODULE_ACTION_RECORDER_CONTACT_US_EMAIL_MINUTES') ? (int)MODULE_ACTION_RECORDER_CONTACT_US_EMAIL_MINUTES : 15)));
    }

    if ($error == false) {
      tep_mail(STORE_OWNER, STORE_OWNER_EMAIL_ADDRESS, EMAIL_SUBJECT, $enquiry, $name, $email_address);

      $actionRecorder->record();

      tep_redirect(tep_href_link(FILENAME_CONTACT_US, 'action=success'));
    }
  }

  $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_CONTACT_US));

  require(DIR_WS_INCLUDES . 'template_top.php');
?>

<h1><?php echo HEADING_TITLE; ?></h1>

<?php
  if ($messageStack->size('contact') > 0) {
    echo $messageStack->output('contact');
  }

  if (isset($HTTP_GET_VARS['action']) && ($HTTP_GET_VARS['action'] == 'success')) {
?>

<div class="contentContainer">
  <div class="contentText">
    <?php echo TEXT_SUCCESS; ?>
  </div>

  <div style="float: right;">

    <?php echo tep_draw_button(IMAGE_BUTTON_CONTINUE, 'triangle-1-e', tep_href_link(FILENAME_DEFAULT)); ?>

  </div>
</div>

<?php
  } else {
?>

<?php echo tep_draw_form('contact_us', tep_href_link(FILENAME_CONTACT_US, 'action=send'), 'post', '', true); ?>

<div class="contentContainer">
  <div class="contentText">
    <table border="0" width="100%" cellspacing="0" cellpadding="2">
      <tr>
        <td class="fieldKey"><?php echo ENTRY_NAME; ?></td>
        <td class="fieldValue"><?php echo tep_draw_input_field('name'); ?></td>
      </tr>
      <tr>
        <td class="fieldKey"><?php echo ENTRY_EMAIL; ?></td>
        <td class="fieldValue"><?php echo tep_draw_input_field('email'); ?></td>
      </tr>
      <tr>
        <td class="fieldKey" valign="top"><?php echo ENTRY_ENQUIRY; ?></td>
        <td class="fieldValue"><?php echo tep_draw_textarea_field('enquiry', 'soft', 50, 15); ?></td>
      </tr>
            <tr>
        <td></td>
        <td>
              <div class="g-recaptcha" data-sitekey="6Lf1EaIUAAAAAHCvCvzM4qpHdxPgCB7fznN0B4ZR"></div>

        </td>
      </tr>
    </table>
  </div>

  <div class="buttonSet">
    <span class="buttonAction"><?php echo tep_draw_button(IMAGE_BUTTON_CONTINUE, 'triangle-1-e', null, 'primary'); ?></span>
  </div>
</div>

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

  require(DIR_WS_INCLUDES . 'template_bottom.php');
  require(DIR_WS_INCLUDES . 'application_bottom.php');
?>


Expected outcome is for the user to HAVE to click on google repcaptcha and have a success response before the user can select the continue button on the form

1 个答案:

答案 0 :(得分:0)

我们还在oscommerce templates中使用了Recaptcha。

  1. 在提交按钮<span class="buttonAction"><?php echo tep_draw_button(IMAGE_BUTTON_CONTINUE, 'triangle-1-e', null, 'primary'); ?></span>之前

包括此文件<?php require_once 'ext/recaptcha/recaptcha.php' ?>

  1. ext / recaptcha / recaptcha.php的内容:

      <?php
      /**
       * Created by PhpStorm.
       * User: 'Serhii.M'
       * Date: 25.02.2019
       * Time: 15:21
       */
    
      if ($_SERVER['HTTP_X_REQUESTED_WITH'] == 'XMLHttpRequest') {
          $rootPath = dirname(dirname(dirname($_SERVER['SCRIPT_FILENAME'])));
          chdir('../../');
          require($rootPath.'/includes/application_top.php');
      }
    
    
    
    
      if (GOOGLE_RECAPTCHA_STATUS === 'true'){
    
      if (isset($_POST['action']) && $_POST['action'] === 'checkResponseToken'){
          $token = $_POST['token'];
          $data = array(
              'secret' => GOOGLE_RECAPTCHA_SECRET_KEY,
              'response' => $token
          );
    
          $verify = curl_init();
          curl_setopt($verify, CURLOPT_URL, "https://www.google.com/recaptcha/api/siteverify");
          curl_setopt($verify, CURLOPT_POST, true);
          curl_setopt($verify, CURLOPT_POSTFIELDS, http_build_query($data));
          curl_setopt($verify, CURLOPT_SSL_VERIFYPEER, false);
          curl_setopt($verify, CURLOPT_RETURNTRANSFER, true);
          $response = json_decode(curl_exec($verify));
          $_SESSION['recaptcha'] = $response->success;
          die(json_encode(1));
      }else{?>
          <script src="https://www.google.com/recaptcha/api.js"></script>
          <script>
              function reCaptchaCallback(callback){
                  $.ajax({
                      url:'./ext/recaptcha/recaptcha.php',
                      dataType:'json',
                      method:'post',
                      data:{'action':'checkResponseToken','token':callback}
                  }).done(function(response){
                  })
              }
          </script>
          <div class="g-recaptcha" data-sitekey="<?=GOOGLE_RECAPTCHA_PUBLIC_KEY?>" data-callback="reCaptchaCallback"></div>
      <?}?>
    
      <?}?>
    

其中GOOGLE_RECAPTCHA_SECRET_KEYGOOGLE_RECAPTCHA_PUBLIC_KEY-您来自Google Recaptcha内阁的钥匙。

  1. $name = tep_db_prepare_input($HTTP_POST_VARS['name']);之前添加以下代码:

        if ($_SESSION['recaptcha'] !== true) {
    
            $messageStack->add('contact', 'reCaptcha error');
            die;
        }
    
  2. $breadcrumb->add(NAVBAR_TITLE, tep_href_link(FILENAME_CONTACT_US));之前添加以下代码:

      unset($_SESSION['recaptcha']);