简体   繁体   中英

Codeigniter 3.1.3 flash_data lost after redirect

Consider this controller method

public function authenticate_session($user, $access_token) {
    $this->output->enable_profiler(TRUE);

    $user = $this->user_model->find_users(array("email" => $user, "access_token" => $access_token), "email, last_activity, access_token");

    if ($user) {
        $this->load->library('session');

        $this->session->set_flashdata('post_data', $this->input->post(NULL));

        redirect("v1/transactions_controller/pay");
    } else {
        redirect("v1/sessions/unauthenticated");
    }
}

And the pay method in the transactions controller

public function pay() {
        $this->output->enable_profiler(TRUE);

        $this->load->library('session');

        var_dump($this->session->flashdata());
}

var_dump($this->session->flashdata()); gives me array(0) { }

I've checked everywhere I know to and everyone seems to be saying that flash_data in the session library is the best option for me. I need to pass those POST parameters from the initial request on to the transactions controller once the user has been authenticated. If I remove the redirect and simply do var_dump($this->session->flashdata()); I see what I expect (POST data from the previous request), but it's not passing after the redirect.

Using redirect("v1/transactions_controller/pay", "refresh"); doesn't seem to actually go to the new controller and instead refreshes in place on the authentication controller method, but I do get to see my flashdata. Am I doing something wrong? Should my server be configured a certain way for this to work properly? I've added session_start() to my index.php, but it threw an error because it's already in the session library, which is loaded by both controllers that need it.

My session and cookie variables in config.php

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

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

Your problem is with the session configuration. When using the files driver $config['sess_save_path'] must be set to an absolute path and the permissions need to be set - probably to 0700. The session data is save in files in the folder you set. No files === no session data.

Also, I suggest this one cookie setting change

$config['cookie_domain'] = '';

Since you seem to be needing this throughout your site I recommend autoloading the session library. This is done in the file application/config/autoload.php . Find the $autoload['libraries] item in the file and set it like this.

$autoload['libraries'] = array('session');

Now you do not have to load it in anywhere else in the application.

The comment in the file explain how to load multiple libraries at once.

For security reasons it is recommended to put the session folder outside of the public html files. Create a folder named "sessions" at the same level as your site's root. That is usually the folder where Codeigniter is installed. Set the permissions of this folder to 0700. The owner will probably need to be "www-data". When in doubt, use the same owner at the htdocs folders.

Assuming that htdocs is your root folder put this in the sessions section of config.php.

$config['sess_save_path'] = substr(FCPATH, 0, strpos(FCPATH, "htdocs/"))."sessions/";

The constant FCPATH is defined by Codeigniter and is the absolute path to your application root - the place where the index.php file is stored. The code strips off "htdocs/" and replaces it with "sessions/". You should be good to go. If your root is not htdocs change "htdocs/" to "yourRootFolderName/" in strpos .

On my testbed server the path defined by FCPATH looks like this

/var/www/htdocs/

So the sessions folder is at

/var/www/sessions/

If you're using WAMP or similar stack on a Windows machine FCPATH might look more like this.

C:\wamp\www\htdocs\

You need to create a path for your sessions

https://www.codeigniter.com/user_guide/libraries/sessions.html#session-preferences

Change

$config['sess_save_path'] = NULL;

To something like below and Make sure you set permission 0700

$config['sess_save_path'] = APPPATH . 'cache/sessions/';

Codeigniter path functions definitions

EXT: The PHP file extension

FCPATH: Path to the front controller (this file) (root of CI)

SELF: The name of THIS file (index.php)

BASEPATH: Path to the system folder

APPPATH: The path to the "application" folder

Also to keep flash data

$this->session->keep_flashdata('item');
$this->session->keep_flashdata(array('item1', 'item2', 'item3'));

And codeigniter session tempdata

https://www.codeigniter.com/user_guide/libraries/sessions.html#tempdata

$config['cookie_domain'] = '.localhost';

CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.

as the very first thing, which obviusly means that you need to do a new server request. A redirect, a refresh, a link or some other mean to send the user to the next request.

Why use flashdata if you are using it in the same request, anyway? You'd might as well not use flashdata or use a regular session.

与大多数没有意义的事情一样,我发现只有一个小错误需要纠正:我需要在config.php中设置base_url();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM