PHP的验证码会话不更新 [英] php captcha session doesn't update

查看:70
本文介绍了PHP的验证码会话不更新的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有以下代码:

captcha.php:

session_start();
class Captcha {
    protected $code;
    protected $width = 35;
    protected $height = 150;

    function __construct() { 
        $this->code = substr(sha1(mt_rand()), 17, 6); 
        $_SESSION['captcha'] = $this->code;
    }

    function getCode(){
        return $this->code;
    }

    function showImage() {
             // here comes the code that builds the image.
             // it works fine!

    }
}


$image = new Captcha();

$image->showImage();

在我的登录表格中,我有:

And in my login form I have:

<iframe src="includes/captcha.php" frameborder="0" height="65" width="180"></iframe>

如果我 print_r($ _ SESSION) $ _ SESSION ['captcha'] 始终保持延迟:它包含以前的验证码代码,而不是当前显示的代码。

if I print_r($_SESSION), the $_SESSION['captcha'] always in delay : it contains the previous captcha code , and not the current which is being shown.

我该怎么办?

推荐答案

<iframe src="includes/captcha.php" frameborder="0" height="65" width="180"></iframe>

应为:

<img src='includes/captcha.php' style='height:65px;width:180px' />

因为您应该将图像作为图像而不是iframe加载。

As you should be loading the image as an image not as an iframe.

如果在父页面上打印出的验证码也始终是旧值,因为新值仅在 captcha.php 已加载,这是在加载主页后发生的,因此当时新的会话值不可用。因此,否则其他一切都正常。

Also your CAPTCHA code if printed out on the parent page will always be the old value, as the new value only gets written when captcha.php is loaded which has happened after the main page has loaded so the new session value was not available at that time. So everything otherwise is working fine.

这篇关于PHP的验证码会话不更新的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆