0

我希望 $code 变量的值出现在我的其他页面上——captcha.php。

captcha_image.php 


$captcha = new CaptchaCode();  //class defined in captcha_code.php
$code = str_encrypt($captcha->generateCode(6));  //function defined in captcha_code.php
$captcha1 = new CaptchaImages();
$captcha1-> GenerateImage($width,$height,str_decrypt($code));


captcha.php

<img style="cursor: pointer;width: 50px;height: 50px;" src="refresh.png" onclick="refresh_captcha();"/>
<input type="hidden" name="security_check" value="<?php echo $code; ?>">  // want value of $code here

<script type="text/javascript">
function refresh_captcha()
{
    var img = document.getElementById('captcha_img');
    img.src = 'captcha_images.php';
    jQuery("#captcha_img").attr("src",img.src);
}
</script>

我不能在我的代码中包含 captcha_images.php 文件,甚至不希望它使用会话来完成,尝试过这种方式。如果有人对此有解决方案,请帮我解决这个问题。

4

2 回答 2

0

更好的解决方案是将代码保存到 SESSION 中。

例如:

captcha_image.php:

session_start();
$captcha = new CaptchaCode();
$code = $captcha->generateCode(6);
$captcha1 = new CaptchaImages();
$captcha1-> GenerateImage($width,$height,$code);
$_SESSION["captchacode"] = $code;

并在提交表单后检查正确性:

session_start();
...
if($_SESSION["captchacode"]!=$_POST["security_check"]){
    echo "Wrong captcha!";
}else{
    // captcha is correct, process the form
}
于 2013-09-10T09:19:23.017 回答
0

如果您无法使用 cookie 和会话,则无法从仅返回图像的 captcha_image.php 获取信息。您必须在 else 请求中生成信息,例如:

<img id="captcha_img" src="captcha_images.php?encoded_code=<?php echo $code ?>" onclick="refresh_captcha();"/>
<input type="hidden" id="captcha_hidden" name="security_check" value="<?php echo $code ?>">
<script type="text/javascript">
function refresh_captcha()
{
    // generate_captcha.php returns only encoded captcha
    $.get('generate_captcha.php', function(encoded_code) {
        $('#captcha_hidden').val(encoded_code);
        $('#captcha_img').attr("src","captcha_images.php?encoded_code="+encoded_code);
    });
}
</script>

这里generate_captcha.php返回编码的验证码,captcha_images.php不生成代码,仅从 hiss 参数中解码代码,encoded_code并且此代码也插入隐藏。

于 2013-09-10T09:31:00.120 回答