清风徐来
Michael's Blog
系统时间不准确造成 IE 下 Kohana 的 Captcha 库验证失败

淡水碰到了,记下来备忘。

作者:Hily 原始链接:http://hily.me/blog/2009/04/system-time-kohana-captcha-fail/ 版权声明:可以转载,转载时务必以超链接形式标明文章原始出处和作者信息及版权声明

好糗……

最近玩 Kohana 碰到太多诡异的问题,比如所有模板都要以 UTF8 无 BOM 的编码格式保存,否则 IE 下显示会有问题。自己写的 helper 类也要保存成 UTF8 无 BOM 的编码格式,否则也会出现类似的问题。

这一系列诡异的问题,把我原本清晰的思路也被搞乱了。

测试代码(使用 Kohana 文档上的):

// Load Captcha library, you can supply the name of the config group you would like to use. $captcha = new Captcha; // Ban bots (that accept session cookies) after 50 invalid responses. // Be careful not to ban real people though! Set the threshold high enough. if ($captcha->invalid_count() > 49) exit(‘Bye! Stupid bot.'); // Form submitted if ($_POST) { // Captcha::valid() is a static method that can be used as a Validation rule also. if (Captcha::valid($this->input->post(‘captcha_response’))) { echo ‘Good answer!'; } else { echo ‘Wrong answer!'; } // Validate other fields here } // Show form echo form::open(); echo ‘Other form fields here…'; // Don’t show Captcha anymore after the user has given enough valid // responses. The “enough” count is set in the captcha config. if ( ! $captcha->promoted()) { echo ‘'; echo $captcha->render(); // Shows the Captcha challenge (image/riddle/etc) echo ‘'; echo form::input(‘captcha_response’); } else { echo ‘You have been promoted to human.'; } // Close form echo form::submit(array(‘value’ => ‘Check’)); echo form::close();

验证代码在 Firefox 下工作正常,在 IE、Chrome 下都不能正常工作,这肯定就是浏览器之间的差别造成的。

跟踪后发现在 IE 下调用:

总是返回空。怎么会取不到 Session 的 Cookie?

用 Fiddler 看了看 Cookie,发现 expires 时间比我当前系统的时间,也就是说 Session 类写了一个过期的时间到 Cookie 中,所以总是取不到 Session 的 Cookie 信息。按我的理解,写入一个过期的 Cookie,本就不应该被取到,而 Firefox 下还能取到这条过期的 Cookie,应该算 FF 的一个 Bug 吧。

怎么会出现过期时间?

因为我的 Kohana 是放在虚拟机里跑的,而我的虚拟机长年都是 suspend 而不是 poweroff,所以时间自然要比当前时间慢 = =

这类诡异的问题在 QQ 群上问是几乎不可能找到答案的,所以关键时刻还是得靠自己解决啊。

– EOF –


最后修改于 2009-06-16