简体   繁体   English

CodeIgniter会话与PHP会话

[英]CodeIgniter sessions vs PHP sessions

I'm relatively new to CodeIgniter and am making my first CI project in which there are user-accounts, etc. In the past, I have always used PHP's $_SESSION variable to this end. 我是CodeIgniter的新手,我正在制作我的第一个CI项目,其中有用户帐户等。过去,我总是使用PHP的$ _SESSION变量。 However, CI seems to have its own session mechanism, which it claims is "better" 然而,CI似乎有自己的会话机制,它声称“更好”

CI's session mechanism seems to store all the data in a cookie? CI的会话机制似乎将所有数据存储在cookie中? Personally I like the idea of all the data being stored on the server, accessed with a cookie-key like PHPs native session mechanism... Am I being dumb thinking that's better? 就个人而言,我喜欢将所有数据存储在服务器上的想法,使用像PHP本机会话机制这样的cookie密钥进行访问...我是不是觉得这样更好? Should I just accept CI's mechanism? 我应该接受CI的机制吗? Or should I go ahead and use native PHP sessions? 或者我应该继续使用本机PHP会话?

What do you guys do? 你们做什么的?

Thanks, 谢谢,
Mala 马拉

In my experience with CI I've encountered some anomalies with its sessions, but for most day-to-day needs the library is good and easy to work with. 根据我对CI的经验,我遇到了一些关于会话的异常,但是对于大多数日常需求,图书馆很好并且易于使用。 As it was pointed out, Flashdata is a very nice feature. 正如有人指出的那样,Flashdata是一个非常好的功能。

If you choose to stay with CI's sessions, I'd strongly suggest to store sessions in a database and, additionally, encrypt cookies: 如果您选择继续使用CI的会话,我强烈建议您将会话存储在数据库中,此外还要加密Cookie:

$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database']   = TRUE;
$config['sess_table_name']     = 'sessions';

The database structure should be as follows: 数据库结构应如下:

CREATE TABLE IF NOT EXISTS  `sessions` (
    session_id varchar(40) DEFAULT '0' NOT NULL,
    ip_address varchar(16) DEFAULT '0' NOT NULL,
    user_agent varchar(50) NOT NULL,
    last_activity int(10) unsigned DEFAULT 0 NOT NULL,
    user_data text NOT NULL,
    PRIMARY KEY (session_id)
);

The manual says more flexibility rather than better ;-) 手册说的更灵活而不是更好 ;-)

I presume the main benefit of CodeIgnite session class is that it integrates with the framework and it offers a few extra functionality, such as IP address tracking and what it calls flashdata (session data that's erased as soon as it's read). 我认为CodeIgnite会话类的主要好处是它与框架集成,它提供了一些额外的功能,例如IP地址跟踪和它所谓的flashdata (会话数据在读取后立即擦除)。 If you are using a framework in the first place that means these options may be attractive for you. 如果您首先使用框架,这意味着这些选项可能对您有吸引力。

Whatever, you can also save session data into a database: 无论如何,您还可以将会话数据保存到数据库中:

http://codeigniter.com/user_guide/libraries/sessions.html http://codeigniter.com/user_guide/libraries/sessions.html

Keep PHP session for important information and use CI session for less important info. 保持PHP会话以获取重要信息,并将CI会话用于不太重要的信息。

Read here wyh. 在这里阅读wyh。 http://codeigniter.com/forums/viewthread/130577/ http://codeigniter.com/forums/viewthread/130577/

I know this is an older post, but I feel it is worth sharing what I have found. 我知道这是一篇较老的帖子,但我觉得值得分享我发现的内容。

Since CI uses a cookie based approach ( even with database storage ) it causes a problem for my particular app which serves data to remote clients requesting data through curl. 由于CI使用基于cookie的方法(即使使用数据库存储),因此导致我的特定应用程序出现问题,该应用程序向通过curl请求数据的远程客户端提供数据。 The bottom line is Cookies and Cross Site Scripting, although manageable, do not play well together. 最重要的是Cookies和Cross Site Scripting,尽管可管理,但不能很好地协同工作。

I chose to try to override the native Session.php class provided by CI with my own MY_Session.php. 我选择尝试使用我自己的MY_Session.php覆盖CI提供的本机Session.php类。 I was happy to find this wasn't too difficult, but was surprised to find that CI was regenerating session id even though my script explicitly provided them. 我很高兴地发现这并不太难,但我很惊讶地发现CI正在重新生成会话ID,即使我的脚本明确提供了它们。

According to the CI manual 根据CI手册

The user's unique Session ID (this is a statistically random string with very strong entropy, hashed with MD5 for portability, and regenerated (by default) every five minutes ) 用户唯一的会话ID(这是具有非常强的熵的统计随机字符串,使用MD5进行散列以实现可移植性,并且每五分钟重新生成(默认情况下)

Although I can probably find a way to override this, I am wondering if it wouldn't be much easier to revert back to PHP sessions. 虽然我可能找到一种方法来覆盖它,但我想知道恢复到PHP会话是否更容易。

Just food for thought if you're going to use CI. 如果您打算使用CI,那只是值得深思。

CI sessions has Storage size limitations CI会话具有存储大小限制

As you are aware , CI sessions are cookies basically, whether you encrypt it or not. 如您所知,CI会话基本上是cookie,无论您是否对其进行加密。 As far as security is concerned both have its on pros and cons. 就安全而言,两者都有其优点和缺点。

My concern was the size limit of CI sessions, It can hold only 4 kb data as its basically a cookie, while Native PHP session only stores reference id on cookie and all the session data is stored in server memory. 我担心的是CI会话的大小限制,它只能容纳4 kb数据,因为它基本上是一个cookie,而Native PHP会话只存储cookie上的引用ID,所有会话数据都存储在服务器内存中。 This comes handy when you have a larger number of items need to be stored in a session. 当您需要在会话中存储大量项目时,这很方便。

Say a shopping cart with more items, or a user music playlist with more than 50 tracks... etc. 说一个包含更多商品的购物车,或者包含超过50首曲目的用户音乐播放列表......等等。

I hope this information helps someone some day. 我希望这些信息有一天可以帮助某人。

Cheers..!! 干杯..!!

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

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