简体   繁体   English

将PHP会话数据存储在Cookie中

[英]Storing php session data in a cookie

I know that the default PHP session storage mechanism is a file, usually in tmp. 我知道默认的PHP会话存储机制是一个文件,通常在tmp中。 And the session_id is carried in a cookie. session_id包含在cookie中。

I want to also store my small amount of session data in the cookie too. 我也想将少量的会话数据存储在cookie中。 (Like Rails does by default.) (默认情况下,像Rails一样。)

Question: Is there a php library to handle this for me or do I need to roll my own using session_set_save_handler etc? 问题:是否有一个PHP库可以为我处理此问题,或者我需要使用session_set_save_handler等自己滚动?

Added: Is just the CodeIgniter session class available? 补充: CodeIgniter会话类可用吗?

What you need to ask yourself is why you are doing this. 您需要问自己的是为什么要这么做。 Provided that you provide your session ID, you can access any information present in the session itself. 只要提供会话ID,就可以访问会话本身中存在的任何信息。 If you prefer to do it any other way, you will have to roll your own session system. 如果您希望以其他方式执行此操作,则必须滚动自己的会话系统。

Here are a couple of points against what you have in mind: 以下是与您的想法有关的几点:

  • Cookies have a maximum size (4kb). Cookies的最大大小为(4kb)。 It is actually pretty common for CI cookies to overflow. CI cookie溢出实际上很常见。
  • The information in the file server-side is guaranteed tamper-proof (or as tamper-proof as your server). 文件服务器端的信息可保证防篡改(或与服务器一样防篡改)。 The information in the cookie is not Cookie中的信息不是
  • If it is the "OMG DISK READ" thing that is scaring you - you can always bind redis as your session manager. 如果让您感到恐惧的是“ OMG DISK READ”,则可以始终将redis绑定为会话管理器。 It is actually trivial to do. 这实际上是微不足道的。
  • Finally, you will have a significant PITA the moment your code gets ported elsewhere 最后,当您的代码移植到其他地方时,您将获得重要的PITA

Assuming you still want to do it (are you really sure?), the answer is just to set another cookie. 假设您仍然想要这样做(确定吗?),答案只是设置另一个Cookie。 PHP session handlers are best not to mess around with due to the significant risk of throwing 500s all over the place if a change goes wrong. PHP会话处理程序最好不要乱搞,因为如果更改出错,则可能会在整个地方抛出500s的巨大风险。 A second cookie is your best bet. 第二个cookie是您最好的选择。

try this 尝试这个

$newdata = array(
                   'username'  => 'johndoe',
                   'email'     => 'johndoe@some-site.com',
                   'logged_in' => TRUE
               );

$this->session->set_userdata($newdata);



$this->input->set_cookie($newdata);

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

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