在CodeIgniter 3.1.2中重定向后,会话数据丢失 [英] Session Data lost after redirect in CodeIgniter 3.1.2

查看:56
本文介绍了在CodeIgniter 3.1.2中重定向后,会话数据丢失的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我仍在开发CodeIgniter应用程序,但我意识到当我登录并在会话中设置用户数据时,它会被设置,但在重定向后会消失。因此,重定向后我无法访问会话数据。

I have a CodeIgniter application still in development but I realise that when I log in and set user data in session, it gets set but disappears after redirecting. Therefore, I cannot access the session data after redirecting.

这是我的登录脚本

public function login(){

    if(!empty($_SESSION['user_id'])){//Meaning you are logged in
        //We inform you
        $this->session->set_flashdata('msg', "<div class='alert alert-success'><span class=''></span> You are already logged in as <strong>".$this->session->user_name."</strong></div>");

        //And send you back to your dashboard
        return redirect('/account/dashboard');
    }

    if($this->input->post()){
    $this->load->library('form_validation');
    $this->form_validation->set_rules('email','Email','required|valid_email');
    $this->form_validation->set_rules('password','Password','required');

        if($this->form_validation->run() == TRUE){

            $creds=['email'=>$this->input->post('email'),'password'=>$this->input->post('password')];
            $this->load->model('user/user_model');

            $user=$this->user_model->login($creds);

            if(!$user){
                $data['error_msg']="Inavlid login details. Please retry";
            }else{

            $user->user_type=(!empty($user->user_type))?:'2';

                $userdata=['user_id'=>$user->id,'user_name'=>$user->name,'user_email'=>$user->email,'user_type'=>$user->user_type];

                $this->session->set_userdata($userdata);
                    $this->session->set_flashdata('msg', '<div class="alert alert-success"><span class="fa fa-check"></span> Logged in as '.$this->session->userdata('user_name').'</div>');

                return redirect('account/dashboard');
            }

        }
    }

    $data['title']="Login";
    $this->load->view('template/auth/header');
    $this->load->view('account/login',$data);
    $this->load->view('template/auth/footer');

}

这是我的config / congig.php文件

and this is my config/congig.php file

$config['base_url'] = 'http://localhost/tmpad/';

$config['allow_get_array'] = TRUE;
$config['enable_query_strings'] = FALSE;
$config['controller_trigger'] = 'c';
$config['function_trigger'] = 'm';
$config['directory_trigger'] = 'd';

$config['encryption_key'] = '4%^&*9799809-nkhdfioup';

$config['sess_driver'] = 'database';
$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']    = 'http://localhost/tmpad/';
$config['cookie_path']      = '/';
$config['cookie_secure']    = FALSE;
$config['cookie_httponly']  = FALSE; 

$config['csrf_protection'] = TRUE;
$config['csrf_token_name'] = 'csrf_test_name';
$config['csrf_cookie_name'] = 'csrf_cookie_name';
$config['csrf_expire'] = 7200;
$config['csrf_regenerate'] = TRUE;
$config['csrf_exclude_uris'] = array();

会话已自动加载到config / autoload.php

session is auto-loaded in config/autoload.php

我将不胜感激。
谢谢

I will appreciate any guidance or assistance. Thank you

推荐答案

我不确定到底是什么问题。最近,我也遇到了这个问题。

I'm not sure what exactly is the problem. Recently I faced this too..

在运行php7.0的开发环境中,此功能以前一直有效。

It was working before in my development running php7.0.

当前它仅在运行nginx和php 5.6的生产服务器中工作。我的开发服务器似乎无法正常工作,并继续在会话表中重新生成新行。我的开发服务器在homestead virtualbox开发环境上使用的是php7.1,通常用于Laravel项目。

Currently it is only working in my production server running nginx and php 5.6. My development server seems to be not working and keeps on regenerate new row in sessions table. My development server is using php7.1, on homestead virtualbox development environment, usually being used for Laravel projects.

我设法通过这一步骤克服了这一点。

I managed to get over this by taking this step.

1)转到system / libraries / Session / Session.php

1) Go to system/libraries/Session/Session.php

2)通过添加/来注释session_start() /。我们要重新定位sessionn_start()。

2) Comment session_start() by adding //. We want to relocate the sessionn_start().

3)向下转到第315行,说安全为王,然后注释掉直到第351行

3) Go down to line 315 where it says Security is king, and comment out until line 351

4)然后转到主index.php(根索引。 php)

4) Then go to your main index.php ( the root index.php )

5)在顶部一次添加session_start()。

5) Add session_start() at the top once.

6)好的,再试一次。希望它能工作。我的猜测是它不能与php 7.1一起使用,并且需要在此Session.php文件中进行一些更新。

6) Okay try again. Hopefully it works. My guess is that it is not working with php 7.1 and some update need to be done in this Session.php file.

我的CI版本是3.1.1

My CI Version is 3.1.1

这篇关于在CodeIgniter 3.1.2中重定向后,会话数据丢失的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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