坚持实施Invisible ReCaptcha

时间:2017-07-11 23:12:23

标签: javascript php recaptcha invisible-recaptcha

我的密码恢复表格是在js函数中:

    function openBeaconPasswordRecovery() {
        $.unblockUI();
        blockMsg(
            '<div>
                Title<br>
                <form>
                    <input placeholder="example@gmail.com" id="recoverEmail" name="recoverEmail" class="txtInput" type="text" onblur="validateMail(this.value)"/>
                    <span id="errorRecoverEmail"></span>
                    <div id="tdRecoverEmail" style="display: none"></div>
                    <div id="recaptcha" class="g-recaptcha" data-sitekey="6LdekygUAAAAANGSgYNCNhDRQpgwT-_ZNKWzlpa4" data-callback="sendPasswordRecovery()" data-size="invisible"></div>
                    <input type="button" value="Send" id="passwordRecoveryButton" id="submit" class="btn_big_entrar">
                    </div>
                </form>
                <script>onload();</script>
            ', 150);
    }

如您所见,电子邮件字段验证是通过onblur事件触发的:

function validateMail(email){
        var reg = /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/;

        if (reg.test(email) == false){
            document.getElementById("tdRecoverEmail").innerHTML = "<img src='../common/images/error.gif'>";
            document.getElementById("errorRecoverEmail").innerHTML = "<br>Invalid email";
            document.getElementById("passwordRecoveryButton").disabled = true;
            return;
        }
        else{
            document.getElementById("tdRecoverEmail").innerHTML = "<img src='../common/images/ok.gif' style='padding-top: 2px'>";
            document.getElementById("errorRecoverEmail").innerHTML = "";
            document.getElementById("passwordRecoveryButton").disabled = false;
        }
    }

如果电子邮件无效,则会禁用提交按钮。

在我放入reCaptcha div之前,我有一个onclick事件,它调用将数据发送到php文件的js函数:

function sendPasswordRecovery(){
        var AJ = new Ajax();
        try{
            AJ.setUrl("../main/forgotPass.php");
            AJ.setVar("sEmail"      , $("#recoverEmail").val());
            AJ.setMethod("POST");
            AJ.setOnLoading(sendPasswordRecoveryLoading);
            AJ.setOnComplete(sendPasswordRecoveryComplete);
            AJ.connect();
            AJ.getData();
        }
        catch(e){
        }
    }

    function sendPasswordRecoveryLoading(){
        blockMsg('Processing...');
    }

    function sendPasswordRecoveryComplete(httpRequest, AJ){
        var resp = httpRequest.responseText;

        reload = 1;
        if ((resp == "2") || (resp == 2)){
            blockMsg('<div><p>Response A</p></div>', 1);
        }
        else{
            blockMsg('<div><p>Response B</p></div>', 1);
        }
    }

如上所示,我按照reCaptcha说明将其放入reCaptcha div。

php函数如下所示:

function sendMail(){

// reCaptcha info
$secret         = "private_key";
$remoteip       = $_SERVER["REMOTE_ADDR"];
$url            = "https://www.google.com/recaptcha/api/siteverify";

// Form info
$response       = $_POST["g-recaptcha-response"];
$sEmail         = $_POST["sEmail"];

// Curl Request
$curl = curl_init();
curl_setopt($curl, CURLOPT_URL, $url);
curl_setopt($curl, CURLOPT_POST, true);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_POSTFIELDS, array(
    'secret' => $secret,
    'response' => $response,
    'remoteip' => $remoteip
));
$curlData = curl_exec($curl);
curl_close($curl);

// Parse data
$recaptcha = json_decode($curlData, true);

if ($recaptcha["success"]){
    $objUser = Users::FindFirst("Users", "email = '" . $sEmail . "'");

    if (!$objUser)
        echo 0;

    else{
        if($objUser->deleted == 1 || $objUser->deleted == "1")
            echo 2;

        else{
            $vUsersMail[$objUser->name . " " . $objUser->surname] = $objUser->email;

            $h = md5($objUser->id);
            $g = md5($objUser->password);

            $params = "h=" . $h . "&g=" . $g . "&uid=" . $objUser->id;

            if (sendMails("ForgotPass",$vUsersMail,"", null, $params))
                echo 1;
            else
            echo 0;
        }
    }
}
else
    echo 0;

exit;}

但是当我点击提交按钮时没有任何反应,请告诉我我哪里错了?

0 个答案:

没有答案