PHP会话问题——captcha/Joomla

PHP会话问题——captcha/Joomla,php,session,joomla,history,captcha,Php,Session,Joomla,History,Captcha,我有一个Joomla组件,它调用一个helper函数来创建captcha图像。禁用sh404时一切正常,但启用sh404时,安全映像的会话变量设置不正确,因此提交表单时会收到“无效验证码”消息。有趣的是,如果你再提交5-6次,它会验证罚款并提交。我已经尝试了我能想到的一切-当我回显会话变量并提交验证码时,似乎会话落后了一步-例如: 如果我第一次提交表单并回显会话变量和提交的代码,那么看起来会话变量没有及时设置-我得到了会话变量的空白值。然后我再次提交表单,会话变量是前一个验证码图像的值。下面是生

我有一个Joomla组件,它调用一个helper函数来创建captcha图像。禁用sh404时一切正常,但启用sh404时,安全映像的会话变量设置不正确,因此提交表单时会收到“无效验证码”消息。有趣的是,如果你再提交5-6次,它会验证罚款并提交。我已经尝试了我能想到的一切-当我回显会话变量并提交验证码时,似乎会话落后了一步-例如:

如果我第一次提交表单并回显会话变量和提交的代码,那么看起来会话变量没有及时设置-我得到了会话变量的空白值。然后我再次提交表单,会话变量是前一个验证码图像的值。下面是生成和验证验证码的代码。谢谢

//Generate Captcha image link
function Captchalink($capid = ''){
    return 'index.php?option=com_mycomponent&view=home&task=newCaptcha&capid='.$capid;
}

function generateCode($characters) {
    /* list all possible characters, similar looking characters and vowels have been removed */
    $possible   = '23456789bcdfghjkmnpqrstvwxyz';
    $code       = '';
    $i          = 0;
    while ($i < $characters) {
        $code .= substr($possible, mt_rand(0, strlen($possible)-1), 1);
        $i++;
    }
    return $code;
}

function CaptchaSecurityImages($capid = '') {

    $font       = dirname(__FILE__).DS."monofont.ttf";
    $width      = 90;
    $height     = 30;
    $characters = 6;
    $session    =& JFactory::getSession();

    //Clean buffers
    while (ob_get_level()) {
       ob_end_clean();
    }

    // start output buffering
    if (ob_get_length() === false) {
       ob_start();
    }
    $code = mycomponentHTML::generateCode($characters);

    /* font size will be 75% of the image height */
    $font_size          = $height * 0.75;
    $image              = @imagecreate($width, $height) or die('Cannot initialize new GD image stream');

    /* set the colors */
    $background_color   = imagecolorallocate($image, 255, 255, 255);
    $text_color         = imagecolorallocate($image, 20, 40, 100);
    $noise_color        = imagecolorallocate($image, 100, 120, 180);

    /* generate random dots in background */
    for( $i=0; $i<($width*$height)/3; $i++ ) {
        imagefilledellipse($image, mt_rand(0,$width), mt_rand(0,$height), 1, 1, $noise_color);
    }

    /* generate random lines in background */
    for( $i=0; $i<($width*$height)/150; $i++ ) {
        imageline($image, mt_rand(0,$width), mt_rand(0,$height), mt_rand(0,$width), mt_rand(0,$height), $noise_color);
    }

    /* create textbox and add text */
    $textbox = imagettfbbox($font_size, 0, $font, $code)  or die('Error in imagettfbbox function');
    $x = ($width - $textbox[4])/2;
    $y = ($height - $textbox[5])/2;
    imagettftext($image, $font_size, 0, $x, $y, $text_color, $font , $code) or die('Error in imagettftext function');

    /* output captcha image to browser */
    header('Content-Type: image/jpeg');
    imagejpeg($image);
    imagedestroy($image);

    /* set session variable for newly created code */
    $session->set('security_code_'.$capid, md5($code));

    ob_end_flush();
    die();

}

function CaptchaValidate($capid = ''){
    $session    =& JFactory::getSession();
    if( $session->get('security_code_'.$capid) == md5(JRequest::getVar('security_code_'.$capid)) ) {
        $session->clear('security_code_'.$capid);
        return true;
    }else{
        return false;
    }
}
//生成验证码图像链接
函数Captchalink($capid=''){
返回'index.php?option=com\u mycomponent&;view=home&;task=newCaptcha&;capid='。$capid;
}
函数生成代码($characters){
/*列出所有可能的字符,相似的字符和元音已被删除*/
$PROPERMIBLE='23456789bcdfghjkmnpqrstvwxyz';
$code='';
$i=0;
而($i<$characters){
$code.=substr($problem,mt_rand(0,strlen($problem)-1),1);
$i++;
}
返回$code;
}
函数CaptchaSecurityImages($capid=''){
$font=dirname(_文件__).DS.“monofont.ttf”;
$width=90;
$height=30;
$characters=6;
$session=&JFactory::getSession();
//清洁缓冲器
while(ob_get_level()){
ob_end_clean();
}
//启动输出缓冲
if(ob_get_length()==false){
ob_start();
}
$code=mycomponentHTML::generateCode($characters);
/*字体大小将是图像高度的75%*/
$font_size=$height*0.75;
$image=@imagecreate($width,$height)或die('cannotinitializenewgdimage-stream');
/*设置颜色*/
$background\u color=imagecolorallocate($image,255,255);
$text_color=imagecolorallocate($image,20,40,100);
$noise_color=imagecolorallocate($image,100120180);
/*在背景中生成随机点*/
对于($i=0;$iget($security_code.'$capid)==md5(JRequest::getVar($security_code.'$capid))){
$session->clear('security\u code.'$capid);
返回true;
}否则{
返回false;
}
}

请发布设置会话的代码,并检查验证码以获取响应-我已经用验证码编辑了我的问题