Codeigniter 会话不适用于 PHP 7
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45756370/
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
Codeigniter session is not working on PHP 7
提问by hrishi
I am using codeigniter version 3.0.6on PHP 5.6and it works fine. but when I run same project on PHP 7.1, codeigniter session is not working. I set session like below
我在PHP 5.6上使用codeigniter 3.0.6 版,它工作正常。但是当我在PHP 7.1上运行相同的项目时,codeigniter 会话不起作用。我设置如下会话
$login_session = $this->session->set_userdata("user_session",$session_data);
when I print $login_sessionit is blank on PHP 7
当我打印 $login_session 时,它在 PHP 7 上是空白的
回答by Shafiq
Recently I've faced the same problem by upgrading my php version from 5.6 to 7.1 My development environment is XAMPP (Apache + MariaDB) using php7.1.4
最近我通过将我的 php 版本从 5.6 升级到 7.1 遇到了同样的问题我的开发环境是使用 php7.1.4 的 XAMPP (Apache + MariaDB)
Please follow the below steps:
请按照以下步骤操作:
1) Go to system/libraries/Session/Session.php
1) 转到 system/libraries/Session/Session.php
2) Comment session_start() by adding //.
2) 通过添加 // 注释 session_start()。
3) Go down to line around 312-315 where it says Security is king, and comment out the following lines:
3) 转到 312-315 附近的行,其中说安全为王,并注释掉以下几行:
ini_set('session.use_trans_sid', 0);
ini_set('session.use_strict_mode', 1);
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.hash_function', 1);
ini_set('session.hash_bits_per_character', 4);
4) Then go to your main index.php ( the root index.php )
4)然后转到您的主要 index.php (根 index.php )
5) Add session_start() at the top once.
5) 在顶部添加 session_start() 一次。
It works for me.
这个对我有用。
回答by Leon Lo
Session id is different in php 7.1 and not pass the regex checking.
会话 id 在 php 7.1 中不同并且没有通过正则表达式检查。
A simple fix: In system/libraries/Session/Session.php arround line 133
一个简单的修复:在 system/libraries/Session/Session.php 中的第 133 行
OR ! preg_match('/^[0-9a-f]{40}$/', $_COOKIE[$this->_config['cookie_name']])
remove '{40}$'
删除“{40}$”
OR ! preg_match('/^[0-9a-f]/', $_COOKIE[$this->_config['cookie_name']])
The problem is that the session id is deleted from cookie because the regex mismatch. So the session id always renew.
问题是会话 id 从 cookie 中删除,因为正则表达式不匹配。所以会话 id 总是更新。
This can fix the regular expression and prevent it run the line "unset($_COOKIE[$this->_config['cookie_name']]);"
这可以修复正则表达式并防止它运行“unset($_COOKIE[$this->_config['cookie_name']]);”行
It totally works for me !
它完全适合我!
FYI. Find this after I fix it myself. https://github.com/bcit-ci/CodeIgniter/issues/4830
供参考。我自己修好后找到这个。 https://github.com/bcit-ci/CodeIgniter/issues/4830
回答by PHP Worm...
I managed to get over this by following these step.
通过遵循这些步骤,我设法克服了这个问题。
1) Go to system/libraries/Session/Session.php
1) 前往 system/libraries/Session/Session.php
2) Search for session_start()
Comment session_start()
by adding //.
2)通过添加 // 来搜索session_start()
评论session_start()
。
3) Go down to line around 312 (or search Security is king
) and comment out all ini_set()
3) 转到 312 附近的行(或搜索Security is king
)并注释掉所有ini_set()
ini_set('session.use_trans_sid', 0);
ini_set('session.use_strict_mode', 1);
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.hash_function', 1);
ini_set('session.hash_bits_per_character', 4);
4) Then go to your main index.php ( the root index.php )
4)然后转到您的主要 index.php (根 index.php )
5) Add session_start()
at the top.
5)session_start()
在顶部添加。
6) Below session_start()
add following lines commented in Session.php
6)在下面session_start()
添加以下注释行Session.php
ini_set('session.use_trans_sid', 0);
ini_set('session.use_strict_mode', 1);
ini_set('session.use_cookies', 1);
ini_set('session.use_only_cookies', 1);
ini_set('session.hash_function', 1);
ini_set('session.hash_bits_per_character', 4);
That's it.
就是这样。
回答by Siddhesh
I found that the issue is with some earlier version of Codeigniter 3 and this is a bug already reported in their website. The bug has been fixed and in the latest version of Codeigniter 3 this issue doesn't happen.
我发现问题出在某些早期版本的 Codeigniter 3 上,这是他们网站上已经报告的错误。该错误已修复,在最新版本的 Codeigniter 3 中不会发生此问题。
If you are already in a faulty version cogeinter consider replacing system folder with latest version's one.
如果您已经在一个有问题的版本 cogeinter 中,请考虑用最新版本的系统文件夹替换系统文件夹。
Found this answer here
在这里找到了这个答案
回答by pimpace
I propose the following steps:
我建议采取以下步骤:
- Go to https://www.codeigniter.com, download the latest 3.x.x version of the framework and unzip it.
- Make a copy of the system folder in the existing app, save it somewhere else then delete the one in your app folder.
- Take a copy of the system folder you just downloaded and place where you deleted the last one from.
- Finish!
- 到https://www.codeigniter.com,下载最新的 3.xx 版本的框架并解压。
- 在现有应用程序中复制系统文件夹,将其保存在其他位置,然后删除应用程序文件夹中的那个。
- 复制您刚刚下载的系统文件夹,并将其放置在您删除最后一个文件夹的位置。
- 结束!
This solves the problem seamlessly, it saves you time as well as the potential risk of messing your app up.
这可以无缝地解决问题,节省您的时间以及搞乱您的应用程序的潜在风险。
回答by Mr. ED
Make sure you have set the session save path
确保您已设置会话保存路径
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_save_path'] = APPPATH . 'cache/session/';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 300;
$config['sess_regenerate_destroy'] = FALSE;
Set the folder permission 0700
设置文件夹权限 0700
Autoload session if you want
如果需要,自动加载会话
$autoload['libraries'] = array('session');
How to set the session
如何设置会话
$session = array('islogged' => true, 'user_id' => '1');
$this->session->set_userdata($session);
Get the user data one set examples
获取用户数据一组示例
echo $this->session->userdata('user_id');
if (!$this->session->userdata('islogged')) {
redirect('logout');
}
回答by Anmol Mourya
Upgrade Codeigniter to latest version. Issue will resolved.
将 Codeigniter 升级到最新版本。问题将得到解决。
回答by Sunday Okoi
The problem is from your PHP version, PHP 7.1 is not really stable, and has a lot of issues with sessions. Change your PHP version to 7.0 and sessions will automatically start working.
问题出在你的 PHP 版本上,PHP 7.1 不是很稳定,而且会话有很多问题。将您的 PHP 版本更改为 7.0,会话将自动开始工作。