简体   繁体   English

我应该将哪个会话库与CodeIgniter一起使用?

[英]Which session library should I use with CodeIgniter?

I have recently started using CI and with it CI sessions, but I have noticed that one thing in particular is much more time consuming to do with CI sessions than with the base PHP sessions: Arrays . 我最近开始使用CI和它的CI会话,但我注意到有一件事特别是使用CI会话比使用基本PHP会话更耗时: 数组

I have an array of data that persists regardless of login/logout called $_SESSION['stats'] , I then store data in that array in the form: 我有一系列数据持续存在,无论登录/注销名为$_SESSION['stats'] ,然后我以下列形式将数据存储在该数组中:

$_SESSION['stats']['last_page'] = $_SERVER['REQUEST_URI']; .

And when a user logs out, it saves the stats array in a variable, clears the session, and then loads it back into the new session. 当用户注销时,它会将stats数组保存在变量中,清除会话,然后将其加载回新会话。

The problem is that in order to edit the last_page key, instead of the one line above, I have to use this code: 问题是,为了编辑last_page键,而不是上面的一行,我必须使用这段代码:

$stats = $this->CI->session->userdata('stats');
$stats['last_page'] = $_SERVER["REQUEST_URI"];
$this->CI->session->set_userdata('stats', $stats);

This is one of a number of annoyances I find in CI sessions, which cause me to feel dissatisfied with it as my session handler. 这是我在CI会话中发现的一些烦恼之一,这使我对它作为我的会话处理程序感到不满。 So my question is: Which session system should I use with CodeIgniter? 所以我的问题是: 我应该在CodeIgniter中使用哪个会话系统? ... is there some reason for using CI sessions? ...是否有使用CI会话的原因? Is there a CI library that you would suggest? 您建议使用CI库吗? Why not just use PHP sessions? 为什么不使用PHP会话?

Thanks, 谢谢,

Lemiant Lemiant

CI sessions offers some extra functionality; CI会话提供了一些额外的功能; such as auto regenerating the session id every given amount of time (for security), IP address tracking, and flashdata (session data that's cleared after it's read once). 例如,每隔给定的时间(安全性),IP地址跟踪和flashdata (在读取一次后清除的会话数据)自动重新生成会话ID。

CI's session mechanism stores all the data in a cookie. CI的会话机制将所有数据存储在cookie中。 PHP's native session mechanism is stored server side. PHP的本机会话机制存储在服务器端。 Each have it's advantages/disadvantages. 每个都有它的优点/缺点。 Cookies can only hold 4KB of data, so if your storing large amounts of data in session PHP native sessions might be better. Cookie只能容纳4KB的数据,因此如果您在会话中存储大量数据,PHP本机会话可能会更好。

If you decide to you want to use native PHP sessions use: Session Hybrid (CI 1.7.2) 如果您决定要使用本机PHP会话,请使用: Session Hybrid (CI 1.7.2)

Session Hybrid uses native PHP sessions, can store session data in the default CI db, is a drop-in replacement for CI's session class, and only requires one file to be rewritten. 会话混合使用本机PHP会话,可以将会话数据存储在默认CI数据库中,是CI会话类的直接替代,只需要重写一个文件。

[* If using a CI version before 1.7.0 try PHPSession and Native Session ] [*如果 1.7.0 之前使用CI版本,请尝试PHPSessionNative Session ]

Side note: If you choose to stay with CI's sessions , for additional security you can store sessions in a database and encrypt the cookies (see Session Preferences ). 附注:如果您选择继续使用CI的会话 ,为了提高安全性,您可以将会话存储在数据库中并加密cookie(请参阅会话首选项 )。

It sounds like you are using a bread crumb method. 听起来你正在使用面包屑方法。

This may help, http://codeigniter.com/forums/viewthread/137949/ 这可能会有所帮助, http://codeigniter.com/forums/viewthread/137949/

And to answer your other question, yes there is a very good reason to use the CodeIgniter session library. 回答你的另一个问题,是的,有一个很好的理由使用CodeIgniter会话库。 I use it because I need to store user session data in my database (safer) and the library comes with the ability to encrypt the cookies and if global XSS filtering is on, then the data will also be scrubbed too. 我使用它是因为我需要在我的数据库中存储用户会话数据(更安全),并且库具有加密cookie的能力,如果启用了全局XSS过滤,那么数据也将被清除。

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

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