的Joomla 2.5 JFactory ::的getSession();似乎是缓存在Firefox [英] Joomla 2.5 JFactory::getSession(); seems to be caching in firefox

查看:110
本文介绍了的Joomla 2.5 JFactory ::的getSession();似乎是缓存在Firefox的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个PHP文件中包含的目录。它的易用性是显示验证码图片。 在该文件中我这样设置会话变量:

I have a php file in includes directory. It's usability is to display a captcha Image. In that file I set a session variable like this:

$code = codegenerator();
$session =& JFactory::getSession();
$session->set('security_code', $code);

此会话变量是从图像中设定的的src 调用,从一个控制器方法。

This Session variable is set from an image src that calls that method from a controller.

然后,我打电话控制器检查所设定的会议 (此方法trigerd从一个iframe AJAX),并在该方法我这样做

Then I call a controller to check that session that was set (this method is trigerd with ajax from an iframe) and in that method I do this

$session = JFactory::getSession();
$seccode=$session->get('security_code');
echo $seccode.':'.rand();

其结果是如预期的第一次,所设定的code和一个随机数。 如果我刷新页面验证码图片被重设一个新的code和被显示出来。 但是,当我再次TRIGER支票时,我得到了previous code有一个新的随机数。 这兰特()有一个证明, JFactory ::的getSession(); 的缓存,因为我得到了新的随机数量,但同样的previous code,而不是像预想的那样新。所以它不是阿贾克斯就是在这里缓存的东西。

The result is as expected the first time, the code that was set and a random number. If I refresh that page the captcha image gets reset with a new code and gets displayed. But when I triger the check event again, I get the previous code with a new random number. That rand() there is a proof that JFactory::getSession(); is cached because I get the new random number but the same previous code and not the new as supposed to. So it's not that ajax that is caching something here.

我怎样才能避免 JFactory ::的getSession(); 歌厅从Firefox缓存? 这种情况只有在firefox。 Internet Explorer和Chrome浏览器似乎正确显示会话code。如果我清除Firefox的现金和刷新页面,它仍然无法正常工作。这就像它的缓存,直到永远。如果我关闭Firefox,然后再次打开它,那么一切似乎工作的第一次,但后来我再有同样的问题。

How can I avoid JFactory::getSession(); geting cached from firefox? This happens only in firefox. Internet explorer and chrome seem to display the session code correctly. If I clear firefox cash and refresh the page it still doesn't work. It's like it's cached for ever. If I close firefox and open it again, then everything seems to work as the first time, but then I have the same issue again.

下面是code产生的验证码

Here is the code that generates the captcha

<?php

defined('_JEXEC') or die('Restricted access');
class CaptchaSecurityImages {

    var $font='monofont.ttf';


    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($width='220',$height='40',$characters='6') {
        $code = $this->generateCode($characters);

        //$font='includes'.DS.'monofont.ttf';
        $font='monofont.ttf';
        $this->font=$font;

        $session =& JFactory::getSession();
        $session->set('security_code', $code);


        /* 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 colours */
        $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, $this->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, $this->font , $code) or die('Error in imagettftext function');
        /* output captcha image to browser */

        header('Content-Type: image/jpeg');
        imagejpeg($image);
        imagedestroy($image);

    }

}
?>

这里是调用的阿贾克斯code

And here is the code that is called by the ajax

public function checkCaptchaSecurityCode(){
        $securitycode = JRequest::getVar('securitycode');           
        $session = JFactory::getSession();
        $seccode=$session->get('security_code');        

        echo $seccode.':'.rand();

        die();
    }   

和这里是Ajax调用

<?php $checkCaptchaSecurityCode = JRoute::_('index.php?option=com_virtuemart&view=participate&task=checkCaptchaSecurityCode&tmpl=component&format=raw'); ?>
    jQuery.ajaxSetup({cache: false});
            jQuery.ajax({
                  type: "POST",
                  url: "<?php echo $checkCaptchaSecurityCode ?>",
                  cache: false,
                  data: { securitycode: jQuery("#security_code").val() }
                }).done(function( msg ) {
                  alert( msg );
            });

请帮忙

推荐答案

我有同样的问题,但设置固定的问题一个新的会话变量之前调用clear方法。

I had the same problem, but calling the clear method before setting a new session variable fixed the problem.

    $session = & JFactory::getSession();
    /* this way unset data from the session store */
    $session->clear('security_code');
    /* and now set the new value */
    $session->set('security_code', $code);

它的工作原理,即使是您第一次声明一个会话变量。

It works even if is the first time you are declaring a session variable.

这篇关于的Joomla 2.5 JFactory ::的getSession();似乎是缓存在Firefox的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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