Php CodeIgniter会话集\u flashdata不工作
我尝试使用Php CodeIgniter会话集\u flashdata不工作,php,session,codeigniter-2,Php,Session,Codeigniter 2,我尝试使用$this->session->set_flashdata('success')重定向到另一个函数后,它不起作用。这是我的密码: <?php class Home extends CI_Controller{ public function __construct(){ parent::__construct(); $this->load->helper(array('url','form'); $this-&g
$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>";
}
}
您实际上需要赋予它一些价值,因此:
$this->session->set_flashdata('welcome');
应该是:
$this->session->set_flashdata('welcome', true);
或者您可以使用完整消息,例如:
$this->session->set_flashdata('welcome', 'Successfully logged in');
等等
请参阅此处有关flashdata的更多信息:来自Codeigniter文档:
CodeIgniter supports "flashdata", or session data that will only be available for the next server request, and are then automatically cleared.
您的问题可能是,当您重定向时,该过程需要多个请求,即清除您的flashdata
使用常规会话或查询参数。我尝试向其中添加$message
,但仍然显示Flashdata失败!走开代码>。我创建的代码如下:$message='Welcome to my site!'
而flashdata是这样的:this->session->setflashdata('welcome',$message)
在我的welcome()
中,我尝试这样回显flashdata:echo$this->sesion->flashdata('welcome')代码>并且没有显示任何内容。可能是因为您在未经过验证的情况下从视图本身重定向到欢迎页面?验证失败时会发生什么?你从来没有指定过。实际上,在login()
form\u validation->run()代码下面还有一些代码。。如果表单验证失败,我将使用以下代码打开主页:$this->template->display('home')
。检查我的更新。尝试将其放入else
条件中,或者在重定向命令后退出,因为有时在重定向后会读取另一行或两行代码,因此最好在重定向后立即停止执行,以防出现更多代码。。。这也可以解释随机性。