我尝试使用$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>";
}
}当我运行该程序时,它显示alert Flashdata Failed! Go Away!,但是我想要插入到数据库中的登录数据被添加到表中。还有一件事,有时flashdata是有效的。从10次尝试中,如果显示Flashdata Failed! Go Away!,则尝试8-9次。有人能告诉我为什么会这样吗?我怎么才能修复它呢?
发布于 2015-01-05 11:39:20
你实际上需要赋予它一些价值,所以:
$this->session->set_flashdata('welcome');应该是:
$this->session->set_flashdata('welcome', true);或者您可以使用完整的消息,例如:
$this->session->set_flashdata('welcome', 'Successfully logged in');等等。
有关flashdata的更多信息,请点击此处:https://ellislab.com/codeigniter/user-guide/libraries/sessions.html
发布于 2015-01-05 12:16:47
来自Codeigniter文档:
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.你的问题可能是,当你重定向时,这个过程需要不止一个请求,那就是清除你的flashdata。
使用常规会话或查询参数。
https://stackoverflow.com/questions/27773066
复制相似问题