php 登录后其他页面中的 Codeigniter 会话数据不可用

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2449118/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 06:32:37  来源:igfitidea点击:

Codeigniter Session Data not available in other pages after login

phpsessioncodeigniter

提问by jswat

So, I have set up a login page that verifies the user's credentials, and then sets codeigniter session data 'email' and 'is_logged_in' and a few other items. The first page after the login, the data is accessible. After that page, I can no longer access the session data. In fact, if I try reloading that first page, the session data is gone.

因此,我设置了一个登录页面来验证用户的凭据,然后设置 codeigniter 会话数据“电子邮件”和“is_logged_in”以及其他一些项目。登录后的第一页,数据可访问。在该页面之后,我无法再访问会话数据。事实上,如果我尝试重新加载第一页,会话数据就会消失。

I have tried storing it in the database, storing it unencrypted (bad idea I know, but it was for troubleshooting), and storing it encrypted. I have autoloaded the session library in config.php.

我尝试将其存储在数据库中,未加密存储(我知道这是个坏主意,但用于故障排除)并加密存储。我已经在 config.php 中自动加载了会话库。

Here's an example of the code I'm using to set the session data:

这是我用来设置会话数据的代码示例:

$data = array(
                    'email' => $this->input->post('username'),
                    'is_logged_in' => true 
                );
                $this->session->set_userdata($data);

And to retrieve it, I'm using :

为了检索它,我正在使用:

$this->session->userdata('email');

Or

或者

$this->session->userdata('is_logged_in');

I've done lots of work with PHP and cookies, and sessions before, but this is my first project with Codeigniter and I'm perplexed.

我以前用 PHP 和 cookie 以及会话做过很多工作,但这是我使用 Codeigniter 的第一个项目,我很困惑。

Could it have something to do with directory issues? I have the login page and process controlled by a 'login' controller, and then it redirects to a 'site' controller.

它可能与目录问题有关吗?我的登录页面和进程由“登录”控制器控制,然后它重定向到“站点”控制器。

Thanks for your help, and please let me know if I need to clarify anything.

感谢您的帮助,如果我需要澄清任何事情,请告诉我。

采纳答案by John McCollum

Your code looks fine. I've had numerous issues with Codeigniter's session library, including something similar to what you mentioned.

你的代码看起来不错。我在 Codeigniter 的会话库中遇到了很多问题,包括与您提到的类似的问题。

Consider looking at the Native Sessionlibrary to resolve your issue.

考虑查看Native Session库来解决您的问题。

回答by Rei

Just so anyone else trying to use CI's sessions has something to try:

因此,任何其他尝试使用 CI 会话的人都可以尝试:

I have been looking around for a solution to the same problem, and after many dead ends, I re-investigated my config.php.

我一直在寻找相同问题的解决方案,经过许多死胡同后,我重新调查了我的config.php.

Therein, I noticed that $config['cookie_domain']wasn't set properly. Upon setting this (and setting my cookie_prefixto ''), sessions now work correctly.

其中,我注意到$config['cookie_domain']没有正确设置。设置此项(并将 my 设置cookie_prefix'')后,会话现在可以正常工作。

Since the cookie needs to be set to your domain in order to be pulled correctly, it was simply not finding a cookie reference for my session, and making a new one every time.

由于需要将 cookie 设置为您的域才能正确提取,因此根本没有为我的会话找到 cookie 引用,并且每次都创建一个新的引用。

(Setting up database sessions helped immensely with figuring this out.)

(设置数据库会话对解决这个问题有很大帮助。)

回答by hohner

If anybody encounters this problem, go to config/config.php and make sure $config['sess_use_database']is set to TRUE. This will save all session data to your database -- which worked for me.

如果有人遇到此问题,请转到 config/config.php 并确保$config['sess_use_database']将其设置为 TRUE。这会将所有会话数据保存到您的数据库中——这对我有用。

If you don't have a table named 'sessions', run this SQL command:

如果您没有名为“sessions”的表,请运行以下 SQL 命令:

CREATE TABLE  `sessions` (
`session_id` VARCHAR( 40 ) NOT NULL DEFAULT  '0',
 `ip_address` VARCHAR( 16 ) NOT NULL DEFAULT  '0',
 `user_agent` VARCHAR( 120 ) DEFAULT NULL ,
 `last_activity` INT( 10 ) UNSIGNED NOT NULL DEFAULT  '0',
 `user_data` TEXT NOT NULL ,
PRIMARY KEY (  `session_id` ) ,
KEY  `last_activity_idx` (  `last_activity` )
) ENGINE = MYISAM DEFAULT CHARSET = utf8;

回答by Antonio Max

Mind to check your cookie name. It can't have dots in it.

请注意检查您的 cookie 名称。里面不能有点。

Bad cookie: .domain.com

错误的 cookie:.domain.com

Good cookie: domaincom

好饼干:domaincom

回答by boursin

Why you don't use ion auth for security and for session data... I always using that when i want to create a software with login. Also you need to create a table in your db with name session. And go to config and database files in Config folder and put true to the session where it wants for the database. You don't have read well the documentation

为什么不使用 ion auth 来保证安全性和会话数据......当我想创建一个带登录的软件时,我总是使用它。您还需要在您的数据库中创建一个名为 session 的表。并转到 Config 文件夹中的配置和数据库文件,并将 true 放入它想要的数据库会话中。你没有很好地阅读文档

回答by Shadab

Codeigniter 3.0 in ubuntu displays only Array ( [__ci_last_regenerate] => 1522220387 )..remaining sessions are not displayedenter image description here

ubuntu 中的 Codeigniter 3.0 仅显示 Array ( [__ci_last_regenerate] => 1522220387 )..未显示剩余会话 在此处输入图像描述

回答by warhead2020

Im also new to code igniter. Juz to give an idea. Have u put session_start() in your controller? here is the example:

我也是代码点火器的新手。Juz给一个想法。你把 session_start() 放在你的控制器中了吗?这是示例:

class Cuser extends Controller {

类 Cuser 扩展控制器 {

function Cuser()
{
    parent::Controller();
    session_start();
    $this->load->helper('url');     
}

}

}

maybe im wrong...but u can try it...:)

也许我错了...但你可以试试...:)