在Codeigniter中重定向后,我的Flash数据消息未通过。我有一个用户控制器:
class User extends CI_Controller {
public function register(){
$data['title']='Register Here';
$this->form_validation->set_rules('name' , 'Name' , 'trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('user_name' , 'Username' , 'trim|required|max_length[32]|callback_check_username_exists'); // or is_unique[users.username]
$this->form_validation->set_rules('password', 'Password','trim|required|min_length[5]|max_length[12]');
$this->form_validation->set_rules('confirm_password', 'Confirm Password','trim|required|min_length[5]|max_length[12]|matches[password]');
$this->form_validation->set_rules('email', 'Email', 'trim|required|valid_email|is_unique[users.email]', array('is_unique'=>'Email Already Used'));
if ($this->form_validation->run()==FALSE){
$this->load->view('templates/header');
$this->load->view('users/register',$data);
$this->load->view('templates/footer');
} else {
$option=array('cost'=>12);
$encrypt_password=password_hash($this->input->post('password'),PASSWORD_BCRYPT,$option);
$this->user_model->add_user($encrypt_password);
$this->session->set_flashdata('user_registered','You are Successfully Registered and Logged In');
// This works print_r($this->session->flashdata('user_registered'));
redirect('posts');
}
}
如上面的注释中所述,在用户控制器中,当我回显它时,会存储闪存数据'user_registered'。
我的POST控制器是:
class Posts extends CI_Controller {
public function index(){
$data['title']='Latest Posts';
$posts=$this->post_model->get_post();
$data['posts']=$posts;
$this->load->view('templates/header');
$this->load->view('posts/index',$data);
$this->load->view('templates/footer');
}
我的标题视图是:
<html>
<head>
<title>My Blog </title>
<link rel="stylesheet" href="<?php echo base_url();?>asset/css/bootstrap.min.css">
<link rel="stylesheet" href="<?php echo base_url();?>asset/css/style.css">
<script src="https://cdn.ckeditor.com/4.11.1/standard/ckeditor.js"></script>
</head>
<body>
<nav class="navbar navbar-expand-lg navbar-dark bg-dark">
<a class="navbar-brand" href="<?php echo base_url();?>">Bloggy</a>
<button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarColor02" aria-controls="navbarColor02" aria-expanded="false" aria-label="Toggle navigation">
<span class="navbar-toggler-icon"></span>
</button>
<div class="collapse navbar-collapse" id="navbarColor02">
<ul class="navbar-nav mr-auto">
<li class="nav-item active">
<a class="nav-link" href="<?php echo base_url();?>">Home </a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>posts">Blog</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>category">Categories</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>about">About</a>
</li>
</ul>
<ul class="nav navbar-nav navbar-right">
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>user/register">Register</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>posts/create">Create Post</a>
</li>
<li class="nav-item">
<a class="nav-link" href="<?php echo base_url();?>category/create">Create Category</a>
</li>
</ul>
</div>
</nav>
<div class="container">
<!-- Flash Data -->
<?php if($this->session->flashdata('user_registered')): ?>
<?php echo $this->session->flashdata('user_registered');?>
<?php else :?>
<?php echo ' no value '; ?>
<?php /*echo '<p class="alert alert-success">'.$this->session->flashdata('user_registered').'</p>'; */?>
<?php endif; ?>
在标题视图中,显示user_registered的flashdata,没有值。只有一个重定向,我应该能够访问Flash数据。一世 我无法弄清楚这个问题。任何帮助表示赞赏。
在进一步调查中,我发现我的会议根本没有进行。我已经自动加载了会话库,并在浏览器中将ci_session视为cookie,但是我测试了重定向后userdata和flashdata均无法正常工作。 Flash数据应该可以进行一次重定向,因为我已经进行了很长时间了。我以前运行良好的应用程序也失败了,因为未存储会话。我创建了如下测试代码:
class User extends CI_Controller {
public function index (){
$sex='M';
$data1['user']=$this->user_model->get_all_users($sex);
$this->session->set_userdata('test','data');
$this->session->set_flashdata('flash', 'data');
print_r($_SESSION); exit();
redirect('projects');
}
我得到了Array([__ci_last_regenerate] => 1547034241 [test] =>数据[flash] =>数据[__ci_vars] => Array([flash] => new))。
现在,我在项目控制器中回显会话:
<?php
class Projects extends CI_Controller {
public function index()
{
print_r($_SESSION); exit();
$result= $this->project->get_projects();
$data['result']=$result ;
$this->load->view('pages/projects',$data);
}
}
我刚刚得到这个:数组([__ci_last_regenerate] => 1547034384)。
My config.php is $config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;
答案 0 :(得分:0)
闪存数据是仅在页面上可用的会话。.当服务器上收到新鲜请求时,它将清空闪存数据...即,您的重定向未设置闪存数据...将user_data设置为获取您的消息...并未设置像
一样拥有它$this->session->userdata('user_registered','You are Successfully Registered and Logged In');
在视图中取消设置
<?php
if($this->session->userdata('user_registered')){
echo $this->session->userdata('user_registered');
$this->session->unset_userdata('user_registered');
}
?>