代码点火器会话set_flashdata不起作用


CodeIgniter session set_flashdata not working

我尝试使用$this->session->set_flashdata('success'),但重定向到另一个函数后它不起作用。这是我的代码:

<?php
class Home extends CI_Controller{
    public function __construct(){
        parent::__construct();
        $this->load->helper(array('url','form');
        $this->load->library(array('session','template','form_validation');
    }
}
/* My another function for form_validation and etc */
public function login(){
    $this->set_login_rules();
    if($this->form_validation->run()){
        /* inserting data to database */
        $this->session->set_flashdata('welcome');
        redirect('home/welcome');
    }
    $this->template->display('home');
}
public function welcome(){
    if($this->session->flashdata('welcome') !== FALSE){
        echo "<script>alert('Flashdata Success! Welcome!</script>";
    }
    else{
        echo "<script>alert('Flashdata Failed! Go Away!');</script>";
    }
}

当我运行该程序时,它显示警报Flashdata Failed! Go Away!但是我要插入数据库的登录数据已添加到表中。还有一件事,有时flashdata正在工作。从 10 次尝试,如果显示 8-9 次尝试Flashdata Failed! Go Away!.谁能告诉我为什么会这样?我该如何修复它?

你实际上需要给它一些值,所以:

$this->session->set_flashdata('welcome');

应该是:

$this->session->set_flashdata('welcome', true);

或者您可以使用完整的消息,例如:

$this->session->set_flashdata('welcome', 'Successfully logged in');

等。。。

在此处查看有关闪存数据的更多信息:https://ellislab.com/codeigniter/user-guide/libraries/sessions.html

来自 Codeigniter 文档:

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

您的问题可能是,当您重定向时,该过程需要多个请求,即清除您的闪存数据。

使用常规会话或查询参数。