问题描述
我在包含目录中有一个 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.
然后我调用控制器来检查设置的会话(此方法是使用 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();
第一次结果和预期一样,设置的代码和一个随机数.如果我刷新该页面,验证码图像将使用新代码重置并显示.但是当我再次触发检查事件时,我得到了带有新随机数的先前代码.rand()
有证据表明 JFactory::getSession();
被缓存了,因为我得到了新的随机数,但之前的代码相同,而不是新的到.所以并不是 ajax 在这里缓存了一些东西.
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 似乎可以正确显示会话代码.如果我清除 firefox cash 并刷新页面,它仍然不起作用.就像它被永远缓存一样.如果我关闭 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.
这是生成验证码的代码
<?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);
}
}
?>
这是ajax调用的代码
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 中缓存的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!