CodeIgniter 会话与 PHP 会话 [英] CodeIgniter sessions vs PHP sessions

查看:25
本文介绍了CodeIgniter 会话与 PHP 会话的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 CodeIgniter 比较陌生,正在制作我的第一个 CI 项目,其中有用户帐户等.过去,我一直为此使用 PHP 的 $_SESSION 变量.然而,CI 似乎有自己的会话机制,它声称它更好"

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. However, CI seems to have its own session mechanism, which it claims is "better"

CI 的 session 机制好像把所有的数据都存储在一个 cookie 中?就我个人而言,我喜欢将所有数据存储在服务器上的想法,使用 cookie 键(如 PHP 的本机会话机制)进行访问……我是不是认为这样更好?我应该接受 CI 的机制吗?还是我应该继续使用原生 PHP 会话?

CI's session mechanism seems to store all the data in a 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? Should I just accept CI's mechanism? Or should I go ahead and use native PHP sessions?

你们是做什么的?

谢谢,
马拉

推荐答案

根据我使用 CI 的经验,我遇到了一些会话异常,但是对于大多数日常需求,该库很好且易于使用.正如有人指出的那样,Flashdata 是一个非常好的功能.

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. As it was pointed out, Flashdata is a very nice feature.

如果您选择继续使用 CI 的会话,我强烈建议将会话存储在数据库中,另外,对 cookie 进行加密:

If you choose to stay with CI's sessions, I'd strongly suggest to store sessions in a database and, additionally, encrypt cookies:

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

数据库结构应该如下:

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)
);

这篇关于CodeIgniter 会话与 PHP 会话的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆