会话在localhost中不起作用,但在实时服务器中起作用(Codeigniter) [英] Sessions not working in localhost but work in live server (Codeigniter)

查看:113
本文介绍了会话在localhost中不起作用,但在实时服务器中起作用(Codeigniter)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

从昨天开始我就一直面临这个问题,但是我无法解决。问题是会话在本地环境中不起作用,但是当出于测试目的,我将相同文件放在实时服务器上时,它们一切正常。

I have been facing this issue since yesterday and yet I could not resolve this. Issue is sessions are not working in local environment but when for a testing purpose I put the same files on a live server, they work all okay.

如果我的 config.php 文件:

$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;

$config['cookie_prefix']    = '';
$config['cookie_domain']    = '';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE;

这是我将数据保存在我的模型文件之一中的方式

Here is how I save the data in one of my model files

$this->session->set_userdata('user',$result); //$result works fine, it produces
right result

在我看来,我尝试过通过以下方式访问它:

In my view, I tried to access this by:

$this->session->userdata['user']['name']; //name here is an element in result array

我收到此错误:


严重性:通知

Severity: Notice

消息:未定义索引:用户

Message: Undefined index: user

文件名:home / home.php

Filename: home/home.php

行号:2

令我惊讶的是,相同的代码在服务器上运行时没有任何错误。

To my surprise, this same code runs without any error on server.

,以便能够知道正在读取的数据由Codeigniter会话保存,我尝试了数据库方法。

Also, to be able to know the data being saved by Codeigniter sessions, I tried database method.

我在 config.php 文件

$config['sess_driver'] = 'database';
$config['sess_cookie_name'] = 'ci_sessions';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;

它不会插入一行数据,而是插入四行数据。请参见下面的数据库屏幕快照(最后四行)。

Instead of inserting one row of data, it inserts four. Please see below screenshot from the database (last four rows).

有人可以指出错误吗?任何帮助将不胜感激。

Can anybody point to the error? Any help will be appreciated.

推荐答案

您的问题有几个部分,我将依次回答。

Your question has a few parts, I'll answer each in turn.

userdata()是一种方法。您访问它的方式将其视为一个数组:

userdata() is a method. The way you are accessing it treats it as an array:

$this->session->userdata['user']['name']; // Bad

如文档所述,您应将要检索的值的键作为参数传递给 userdata 方法:

As the docs describe, you should pass the key of the value you want to retrieve as a parameter to the userdata method:

$user = $this->session->userdata('user');
// Now you can echo $user['name'];

使用更新的语法更简单:

Even simpler is to use the newer syntax:

$user = $this->session->user;
// Now you can echo $user['name'];



关于数据库中的4个会话记录



检查时间戳字段。它们显示3个不同的时间,这意味着这些是网站上的3个独立事件。第一次出现在 1501911275 ,然后6秒后又有2条记录,然后139秒后是最后一条记录。这些代表对网站的3个单独请求-可能您访问的是不同的页面,或者正在测试和重新加载。

Regarding the 4 session records in the database

Check the timestamp fields. They show 3 different times, meaning those are 3 separate events on the site. The first occurs at 1501911275, then 6 seconds later there are another 2 records, and then 139 seconds later the last record. Those represent 3 separate requests to the site - probably you were visiting different pages, or testing and reloading.

关于为什么的记录有多个,这是因为会话ID会定期重新生成。确切的发生频率取决于您的配置,请参见 sess_time_to_update 配置选项。销毁它们的频率还取决于您的配置(请参见文档的同一部分中的 sess_regenerate_destroy )-但通常,较旧的会话记录会保留一段时间并在以后清理通过垃圾收集。因此,尽管您只有1个当前会话记录,但旧记录可以保留一段时间。

As to why there are multiple records, that's because the session ID is periodically regenerated. Exactly how often that happens depends on your configuration see the sess_time_to_update config option. How often they are destroyed also depends on your config (see sess_regenerate_destroy in the same section of the docs) - but normally older session records stick around for a while and are cleaned up later by garbage collection. So while you have only 1 "current" session record, old records can persist for a while.

从您的问题的措辞来看,我认为您想使用 files 会话驱动程序,并且只尝试了数据库驱动程序进行测试。如果是正确的话,则应该修复文件会话配置中的问题。

From the wording of your question I think you want to use the files session driver, and only tried the database driver for testing. If that's correct, there is a problem in your files session configuration which you should fix.

根据文档


仅绝对$ config ['sess_save_path']支持路径。

only absolute paths are supported for $config['sess_save_path'].

config / config.php 也要说:


警告:仅支持绝对路径!

WARNING: Only absolute paths are supported!

您的配置指定会话数据应保存在 ci_sessions 中,这不是绝对路径:

Your configuration specifies that session data should be saved in ci_sessions, which is not an absolute path:

$config['sess_save_path'] = 'ci_sessions'; // Bad

我不确定如何解释,但我猜是目录不会存在,并且Web服务器将无权创建它。我无法想象为什么它可以在您的实时服务器上运行。

I am not sure how will be interpreted, but my guess is the directory won't exist, and the web server will not have permissions to create it. I can't imagine why it works on your live server.

将路径更改为绝对路径,并确保您的Web服务器可以对其进行写入。 再次从文档中获得

Change the path to an absolute path, and make sure your web server can write to it. Again from the docs

mkdir /<path to your application directory>/sessions/
chmod 0700 /<path to your application directory>/sessions/
chown www-data /<path to your application directory>/sessions/

(更改 www-data 发送给您的Web服务器运行用户)。然后:

(change www-data to the user your web server is running as). And then:

$config['sess_save_path'] = '/<path to your application directory>/sessions/';

这篇关于会话在localhost中不起作用,但在实时服务器中起作用(Codeigniter)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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