php 在 Codeigniter 中重定向后会话丢失
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12067929/
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
Session lost after redirect in Codeigniter
提问by Zatanna
I'm using codeigniter as a framework.
我使用 codeigniter 作为框架。
Firstly, I use localhostbut when I change to my IP address the login function doesn't work anymore. I found that the session is lost after redirect to another controller.
首先,我使用localhost但是当我更改为我的 IP 地址时,登录功能不再起作用。我发现重定向到另一个控制器后会话丢失了。
Here is the result of print_r($this->session->all_userdata());
这是结果 print_r($this->session->all_userdata());
[session_id] => 7b00fa0f8790f48d24446f9eb4e6aab2
[ip_address] => 10.42.68.71
[user_agent] => Mozilla/5.0 (Windows NT 5.1) AppleWebKit/537.1 (KHTML, like Gecko) Chrome/21.0.1180.83 Safari/537.1
[last_activity] => 1285962398
[user_data] =>
As you can see it doesn't have any data passed to user_databut it was set before redirect when I test.
正如您所看到的,它没有传递任何数据,user_data但它是在我测试时在重定向之前设置的。
I separate the controller to two which the first one is users -> handler of login function and another is planner which handler of the link that I redirect from users controller.
我将控制器分成两个,第一个是用户 -> 登录功能的处理程序,另一个是规划器,我从用户控制器重定向的链接的处理程序。
users.php(first controller):
users.php(第一个控制器):
$this->load->model('users_model');
$this->load->model('mymodel');
$this->load->database();
$email = $this->input->post('email');
$pass = $this->input->post('password');
$type = $this->input->post('type');
// Authenticate the user
$userdata = $this->users_model->auth_user($email,$pass,$type);
if($userdata)
{
$data = array('FIRSTNAME' => $userdata->FIRSTNAME,
'LASTNAME' => $userdata->LASTNAME,
'EMAIL' => $userdata->EMAIL,
'LOGIN' =>TRUE, 'TYPE' => $type);
$this->session->set_userdata($data);
redirect('planner/view_system','refresh');
}
planner.php(second controller):
planner.php(第二个控制器):
function __construct() {
parent::__construct();
if ( ! ($this->session->userdata('LOGIN')))
{
print_r (var_dump($this->session->userdata('FIRSTNAME')));
print_r($this->session->all_userdata());
}
$this->load->helper(array('form','html','url'));
And here is my config
这是我的配置
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = FALSE;
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE;
$config['sess_match_useragent'] = FALSE;
$config['sess_time_to_update'] = 300;
回答by user1685398
First, you must make sure that there are no special characters in the session items like '\n' or '\v'. Those characters may lead your string to break in the middle. Try trim()for help.
首先,您必须确保会话项中没有诸如“\n”或“\v”之类的特殊字符。这些字符可能会导致您的字符串在中间中断。尝试使用trim()寻求帮助。
If that's no use, maybe it's some encoding problem. Try to encrypt the session item before you set it, and decrypt it when you need to use it.
如果那没有用,可能是一些编码问题。尝试在设置之前对会话项进行加密,并在需要使用时对其进行解密。
回答by katwekibs
I solved this problem by configuring the $config['cookie_domain'] to localhost
我通过将 $config['cookie_domain'] 配置为 localhost 解决了这个问题
$config['cookie_domain'] = "localhost";
i initially had that variable set to fully qualified domain name such as www.exampledomain.com but meanwhile i was using a local server.
我最初将该变量设置为完全限定的域名,例如 www.exampledomain.com,但同时我使用的是本地服务器。
The domain that your script is running under should be the same as the domain set under $config['cookie_domain] to avoid unexpected failure with codeigniter session class.
运行脚本的域应该与 $config['cookie_domain] 下设置的域相同,以避免 codeigniter 会话类出现意外失败。
回答by Dmitriy
I am have similar problem, and it turned that problem is very the commonplace ...
我有类似的问题,结果这个问题很常见......
root of the evil of my problems were the names containing the character "_"
我问题的根源在于包含字符“_”的名称
example, before was
例如,之前是
$this->session->set_flashdata('message_success', 'some message');
after became
成为后
$this->session->set_flashdata('messagesuccess', 'some message');
My problem solved Thanks man with this resource http://biostall.com/losing-codeigniter-sessions/
我的问题解决了谢谢这个资源http://biostall.com/losing-codeigniter-sessions/
回答by Shuvro
You should add one thing to your autoload.phpfile.
您应该在autoload.php文件中添加一件事。
$autoload['libraries'] = array('database','session');
$autoload['libraries'] = array('database','session');
this will solve your session re-direction problem
这将解决您的会话重定向问题
回答by mbdrian
I have the exact same problem and here's what I do.
First, go to file system/libraries/Session/Session.php.
Comment session_start();on line 140.
On function _configure, comment few lines under // Security is king
我有完全相同的问题,这就是我所做的。首先,转到文件system/libraries/Session/Session.php. 评论session_start();上线140。上function _configure,评论下几行// Security is king
// 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);
Second, go to file index.phpon your root project.
Put, session_start();below <?php
其次,转到index.php根项目上的文件。放,session_start();下面<?php
Hope this helps.
希望这可以帮助。
回答by itsols
I was shocked at first when I encountered this problem. It happened to me because my session data had an @ sign.
当我遇到这个问题时,我一开始很震惊。它发生在我身上,因为我的会话数据有一个 @ 符号。
The solution is to simply encode the data using base64_encode.
解决方案是简单地使用 base64_encode 对数据进行编码。
Eg:
例如:
$Data = urlencode( base64_encode($Email) );
$this->session->set_userdata('Data', $Data);
And to reuse it, simply do this:
要重用它,只需执行以下操作:
$Data = base64_decode( urldecode( $this->session->userdata('Data') ) );
Hope this helps.
希望这可以帮助。
NEW FINDING:This problem still persists on somebrowsers and devices. So this is not really a solution anymore. For example, it works on Samsung Android, MicroMAX Android but not on Huawei Android.
新发现:此问题在某些浏览器和设备上仍然存在。所以这不再是真正的解决方案。例如,它适用于三星 Android、MicroMAX Android,但不适用于华为 Android。
回答by Syed Arif Iqbal
It's too late but i was facing same problem luckily i solve this problem by change little bit on configurations as below.
为时已晚,但幸运的是我遇到了同样的问题,我通过对以下配置进行了一些更改来解决此问题。
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie'] = TRUE; // change this
$config['sess_use_database'] = FALSE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = TRUE; // change this
$config['sess_match_useragent'] = TRUE; // change this
$config['sess_time_to_update'] = 300;
Hope this would work for you too.
希望这对你也有用。
回答by bhaveshkac
Same issue with me.
和我一样的问题。
Please check have you load session library in controller.
请检查您是否在控制器中加载了会话库。
$this->load->library('session');
回答by Amin Balogun
Please check your links and make sure they are written correctly. When writing absolute URLs, make sure you add the 'www' part. This was the problem in my case. I hope this helps someone.
请检查您的链接并确保它们写得正确。编写绝对 URL 时,请确保添加“www”部分。这就是我的问题。我希望这可以帮助别人。
Good: header("Location: http://www.yourdomain.com/controller/page");
好:header("位置:http: //www.yourdomain.com/controller/page");
Bad (breaks your session): header("Location: http://yourdomain.com/controller/page");
坏(中断您的会话): header("Location: http://yourdomain.com/controller/page");
回答by mohonid1
From my experience, the problems can be:
根据我的经验,问题可能是:
- one (or more) of your files ain't 'UTF-8 with BOM' (if your website won't be only English and use UTF-8 as encoding like mine). You must change all the files to 'UTF-8 with BOM'. Notepad++ will help.
- there are some characters before php tag or after ?> (better take ?> out). Please be sure there are no any characters include space.
- When call localhost/xxxxxx as the address, change it to 127.0.0.1/xxxxxx or your LAN IP
- I always store sessions in DB instead of file (easier for debuging).
- 您的一个(或多个)文件不是“带有 BOM 的 UTF-8”(如果您的网站不仅是英文并且像我一样使用 UTF-8 作为编码)。您必须将所有文件更改为“带有 BOM 的 UTF-8”。Notepad++ 会有所帮助。
- 在php标签之前或之后有一些字符?>(最好去掉?>)。请确保没有任何字符包括空格。
- 调用 localhost/xxxxxx 作为地址时,将其更改为 127.0.0.1/xxxxxx 或您的局域网 IP
- 我总是将会话存储在数据库而不是文件中(更容易调试)。

