我有一个包含目录中的PHP文件。它的可用性是显示验证码图像。 在该文件I设置这样一个会话变量:Joomla 2.5 JFactory :: getSession();似乎是缓存在Firefox中
$code = codegenerator();
$session =& JFactory::getSession();
$session->set('security_code', $code);
此会话变量是从调用从一个控制器,其方法的图像src
设置。
然后我所说的控制器,以检查被设定 (此方法是trigerd与来自一个iframe AJAX),该会话,并在该方法我为此
$session = JFactory::getSession();
$seccode=$session->get('security_code');
echo $seccode.':'.rand();
结果如预期在第一时间,设置的代码和一个随机数。 如果我刷新该页面,则验证码图像将重置为新代码并显示。 但是当我再次触发检查事件时,我得到一个新的随机数字的代码。 那rand()
有一个证明JFactory::getSession();
被缓存,因为我得到了新的随机数,但是相同的先前的代码,而不是新的假设。所以并不是说Ajax就是在这里缓存某些东西。
如何避免JFactory::getSession();
通过Firefox进行缓存? 这只发生在Firefox。 Internet Explorer和Chrome似乎正确显示会话代码。如果我清除firefox现金并刷新页面,它仍然不起作用。就像它被永久缓存一样。如果我关闭Firefox并再次打开它,那么所有内容似乎都是第一次运行,但是我又遇到了同样的问题。
这里是生成的验证码
<?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
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);
});
请帮忙
这是一个非常奇怪的错误,因为浏览器实际上不应该有任何这些数据。所有会话数据都存储在服务器上,只有会话ID被发送到客户端。我能想到的最好的办法是在重新声明一个'$ session = clear('security_code')',看看它是否会影响这个值。 – 2012-08-20 22:48:32
有没有人找到解决办法? – max4ever 2012-10-11 07:55:37
我实际上做的是每次拨打电话时都添加一个参数ID。这是我如何解决我的问题。这是一个例子: $ checkCaptchaSecurityCode = JRoute :: _('index.php?选项= com_virtuemart&视图=参加&任务= checkCaptchaSecurityCode&TMPL =组分&格式=原始版本&= 1' ); 并且每次我拨打电话时,我都将ver更改为2,然后再次更改为1。这是我欺骗会议的方式,但这并没有解决这个问题,它只是一个工作方式 – themis 2012-11-07 10:24:40