首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >jQuery captcha匹配

jQuery captcha匹配
EN

Stack Overflow用户
提问于 2014-12-30 11:40:15
回答 1查看 1.1K关注 0票数 2

在添加代码以验证匹配后,我有一个jQuery和php,它不起作用。

这是我的jQuery代码

代码语言:javascript
复制
$(document).ready(function() {
$('img#captcha-refresh').click(function() {
    change_captcha();
    });

    function change_captcha(){
        document.getElementById('captcha').src="reCaptcha/get_captcha.php?rnd=" + Math.random();
    }

    var captchascr = $('#captcha').attr('src');

     $('#captcha-code').on('change', function(){
         var captchaval = $(this).val();
            if (captchascr == captchaval) {
                console.log('match');
                }else {
                    console.log('No match');
                    }
         });
});

html

代码语言:javascript
复制
    <div class="captcha-box"> 
        <img src="reCaptcha/get_captcha.php" id="captcha" />
    </div>
    <div class="text-box">
        <label>Type the two words:</label>
        <input name="captcha-code" type="text" id="captcha-code">
    </div>
    <div class="captcha-action">
        <img src="reCaptcha/refresh.jpg" id="captcha-refresh" />
    </div>

下面是php代码

代码语言:javascript
复制
session_start();
$word_1 = '';
for ($i = 0; $i < 4; $i++) 
{
    $word_1 .= chr(rand(97, 122));
}
for ($i = 0; $i < 4; $i++) 
{
    $word_2 .= chr(rand(97, 122));
}
$_SESSION['random_number'] = $word_1.' '.$word_2;
$dir = 'fonts/';
$image = imagecreatetruecolor(165, 50);
$font = "recaptchaFont.ttf"; // font style
$color = imagecolorallocate($image, 0, 0, 0);// color
$white = imagecolorallocate($image, 255, 255, 255); // background color white
imagefilledrectangle($image, 0,0, 709, 99, $white);
imagettftext ($image, 22, 0, 5, 30, $color, $dir.$font, $_SESSION['random_number']);
header("Content-type: image/png");
imagepng($image)

这里的会话是在php文件中创建的,我只是不知道如何调用会话,所以我进行了匹配,有人能帮忙吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-12-30 12:46:25

对名为AJAX的新文件进行validate_captcha.php调用。

文件中的代码处理AJAX调用

代码语言:javascript
复制
    <?php
    session_start();
    $task       = !empty($_POST['task']) ? $_POST['task'] : null;
    $response   = 'false';
    if ( $task == 'validateCaptha' ){
        $inputCaptcha       = !empty($_POST['inputCaptcha']) ? $_POST['inputCaptcha'] : null;
        $sessionCaptcha     = $_SESSION['random_number'];

        if ($inputCaptcha   == $sessionCaptcha){
            $response   = 'true';
        }else{
            $response   = 'false';
        }
        echo $response; 
        exit();
    }

?>

jQuery AJAX代码

代码语言:javascript
复制
    $(document).ready(function() {
    $('img#captcha-refresh').click(function() {
        change_captcha();
        });

        function change_captcha(){
            document.getElementById('captcha').src="reCaptcha/get_captcha.php?rnd=" + Math.random();
        }

$('#captcha-code').on('change', function(){
    var inputCaptcha = $(this).val();
    $.ajax({
    url: 'reCaptcha/validate_captcha.php',
    type: 'POST',
    async: false,
    data: {'task':'validateCaptha', 'inputCaptcha':inputCaptcha},
    success: function(response){
        if (response=='true'){
            alert('Match');
        }else{
            alert('Not Match');
        }
    }

});
         });
});

希望这能帮到你。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/27704806

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档