简体   繁体   English

CodeIgniter 正在数据库中生成多个会话。 为什么?

[英]CodeIgniter is generating multiple sessions in the database. Why?

I have a site which does a few ajax calls on page load.我有一个网站,它在页面加载时执行一些 ajax 调用。 For some reason, CodeIgnitor is inserting 4 sessions (I'm assuming one for each ajax call) as you load the page.出于某种原因,CodeIgnitor 在您加载页面时插入 4 个会话(我假设每个 ajax 调用一个会话)。 I'm storing the sessions in the database.我将会话存储在数据库中。

I'm pretty sure there should only be one session per browser.我很确定每个浏览器应该只有一个会话。 Firefox seems to generate only one; Firefox 似乎只生成一个; other browsers seem to create a whole bunch of sessions.其他浏览器似乎创建了一大堆会话。 Multiple sessions for the same user are giving me some serious authentication problems.同一用户的多个会话给我带来了一些严重的身份验证问题。

Why is this happening?为什么会这样? How can I stop it?我怎样才能阻止它?

I know the discussion took place while ago, but somebody might find this useful.我知道讨论发生在前一段时间,但有人可能会觉得这很有用。

By now I've used CI session without storing its data in database.到目前为止,我已经使用了 CI 会话,而没有将其数据存储在数据库中。 Today I decided to give it a try and immediately run across the same problem: CI was generating new session in every page load.今天我决定试一试并立即遇到同样的问题:CI 在每个页面加载时生成新会话。

I checked my server time, timezone, my cookie etc. - everything I could find as a tip on forums - with no result.我检查了我的服务器时间、时区、我的 cookie 等 - 我可以在论坛上找到的所有提示 - 没有结果。 Then decided to debug the CI Session class myself.然后决定自己调试CI Session类。

Long story short, it turned out that my user_agent field in my session table was too small - VARCHAR 50 - which cuts the original user_agent string - hence CI doesn't find my session and generates onother one.长话短说,事实证明我的会话表中的user_agent字段太小了 - VARCHAR 50 - 它切断了原始的 user_agent 字符串 - 因此 CI 没有找到我的会话并生成另一个会话。 I just increased the user_agent field size to 200 and everything works like a charm.我只是将 user_agent 字段大小增加到 200,一切都像魅力一样。

I forgot to mention that I use Mac OS X Lion.我忘了提到我使用 Mac OS X Lion。

Again, hope this will help somebody.再次,希望这会帮助某人。

Check the date / time on your client OS, and on your server.检查客户端操作系统和服务器上的日期/时间。

I know its too late, but maybe someone finds this page while looking for the answer...我知道为时已晚,但也许有人在寻找答案时找到了此页面...

I think it happens because CI sets an expiration time on the cookie containing the session id and if the time difference between the server and client is higher than the expiration time the cookie gets old and the server will generate a new session for the client on every request.我认为这是因为 CI 在包含会话 id 的 cookie 上设置了一个过期时间,如果服务器和客户端之间的时间差高于 cookie 变旧的过期时间,服务器将为客户端生成一个新的会话要求。 Never took the time to figure out the exact mechanism, but happened to me several times, and this fix always worked.从来没有花时间弄清楚确切的机制,但在我身上发生过几次,而且这个修复总是有效的。

I've found this topic with same problem: on every page CI generates new session.我发现这个话题有同样的问题:在每个页面上 CI 生成新的会话。 Possible solution: remove underscored from site name ( NOT "my_test_site.com", but "my-test-site.com").可能的解决方案:从站点名称中删除下划线(不是“my_test_site.com”,而是“my-test-site.com”)。 At least, this helped in my situation.至少,这对我的情况有所帮助。

Check your config.php file and make sure the cookie options are properly filled out.检查您的 config.php 文件并确保正确填写了 cookie 选项。 If they are not it cant track the user and it will gen a new session on every page load.如果不是,则无法跟踪用户,并且会在每个页面加载时生成一个新会话。

Check the date / time on your client OS, and on your server.检查客户端操作系统和服务器上的日期/时间。

I had the same situation and confirm the solution as a fix我遇到了同样的情况并确认解决方案作为修复

$config['cookie_domain'] = "example.com";

在此代码段中使用您的域名。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM