在PHP服务器端提交表单后重置reCaptcha

时间:2019-02-12 13:34:42

标签: php recaptcha server-side

我有这个脚本,可以正常工作。我想在提交后重置Google的Recaptcha。我知道这很简单,但是我无法在脚本中使用JS函数。

<?php

session_cache_limiter( 'nocache' );
header( 'Expires: ' . gmdate( 'r', 0 ) );
header( 'Content-type: application/json' );
$to         = 'hi@shanemuirhead.co.uk';  // put your email here
$email_template = 'simple.html';
$subject    = strip_tags($_POST['vsubject']);
$email       = strip_tags($_POST['vemail']);
$name       = strip_tags($_POST['vname']);
$message    = nl2br( htmlspecialchars($_POST['vmessage'], ENT_QUOTES) );
$result     = array();


    if(empty($email)){
        $result = array( 'response' => 'error', 'empty'=>'email', 'message'=>'<strong>Error!</strong>&nbsp; Email is empty.' );
        echo json_encode($result );
        die;
    } 
    if(empty($name)){
        $result = array( 'response' => 'error', 'empty'=>'name', 'message'=>'<strong>Error!</strong>&nbsp; Name is empty.' );
        echo json_encode($result );
        die;
    } 
    if(empty($message)){
         $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Message body is empty.' );
         echo json_encode($result );
         die;
    }
    if(empty($_POST['g-recaptcha-response']))  {
        $result = array( 'response' => 'error', 'empty'=>'message', 'message'=>'<strong>Error!</strong>&nbsp; Please complete the Captcha.' );
         echo json_encode($result );
         die;
    }

    if(isset($_POST['g-recaptcha-response']) && !empty($_POST['g-recaptcha-response'])) {
        $secret = '6LeSSI0UAAAAAFnwmu5cj7bk4guiaoXXnlID5-C2';
        $verifyResponse = file_get_contents('https://www.google.com/recaptcha/api/siteverify?secret='.$secret.'&response='.$_POST['g-recaptcha-response']);
        $responseData = json_decode($verifyResponse);
        if($responseData->success == true) {
    $headers  = "From: " . $name . ' <' . $email . '>' . "\r\n";
    $headers .= "Reply-To: ". $email . "\r\n";
    $headers .= "MIME-Version: 1.0\r\n";
    $headers .= "Content-Type: text/html; charset=UTF-8\r\n";
    $templateTags =  array(
        '{{subject}}' => $subject,
        '{{email}}'=>$email,
        '{{message}}'=>$message,
        '{{name}}'=>$name
        );
    $templateContents = file_get_contents( dirname(__FILE__) . '/inc/'.$email_template);
    $contents =  strtr($templateContents, $templateTags);
    if ( mail( $to, $subject, $contents, $headers ) ) {
        $result = array( 'response' => 'success', 'message'=>'<strong>Thank You!</strong>&nbsp; Your email has been delivered.' );

    } else {
        $result = array( 'response' => 'error', 'message'=>'<strong>Error!</strong>&nbsp; Can\'t Send Mail.'  );
    }

    echo json_encode( $result );
    die;

        }
        else
        {
            echo "<p> Sorry verification valid</p>";
        }
   }

?>

我想在回显“谢谢消息发送”之后让脚本重设Recaptcha。

可以通过此功能完成 grecaptcha.reset(); 只需要知道如何在我的脚本中正确放置它即可。 我看过这样的例子:

echo '<script type="text/javascript">',
     'grecaptcha.reset();',
     '</script>'
;

但是无法使其在我的脚本中正常工作。

非常感谢您的帮助或指导。

1 个答案:

答案 0 :(得分:0)

在AJAX成功函数中,我向此代码中添加了此代码,以解决我的问题:

grecaptcha.reset(); //Resets reCaptcha