当会话超时时,设置 Flash数据消息的最佳方式是什么,然后才能在登录页面上收到消息。
我有一个警告变量想要使用,但似乎无法使其与会话超时我将其重定向到一个会话超时的登录页面。
但不确定使用我的错误数组设置Flash数据的最佳方法,任何想法。
登录控制器:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
class Login extends MX_Controller {
private $error = array();
public function __construct() {
parent::__construct();
$this->load->library('user');
$this->load->library('form_validation');
$this->lang->load('common/login', 'english');
}
public function index() {
$this->form_validation->set_rules('username', 'Username', 'required|min_length[4]|max_length[12]');
$this->form_validation->set_rules('password', 'Password', 'required|xss_clean');
if($this->form_validation->run($this) == false) {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (null !==($this->session->flashdata('message_name'), $this->error)) {
$data['message'] = $this->session->set_flashdata('message_name', 'This is my message');
} else {
$data['message'] = '';
}
$this->load->view('common/login', $data);
} else{
if($this->validate()) {
redirect('dashboard');
} else {
$data['title'] = $this->lang->line('heading_title');
$data['text_heading'] = $this->lang->line('text_heading');
if (array_key_exists('warning', $this->error)) {
$data['error_warning'] = $this->error['warning'];
} else {
$data['error_warning'] = '';
}
if (null !==($this->session->flashdata('message_name'), $this->error)) {
$data['message'] = $this->session->set_flashdata('message_name', 'This is my message');
} else {
$data['message'] = '';
}
$this->load->view('common/login', $data);
}
}
}
function validate() {
$username = $this->input->post('username');
$password = $this->input->post('password');
if ($this->user->login($username, $password)) {
return true;
} else {
$this->error['warning'] = $this->lang->line('error_login');
return !$this->error;
}
}
}
登录视图
<?php echo modules::run('common/header/index');?>
<div class="container">
<div class="row">
<div class="col-lg-6 col-lg-offset-3 col-md-4 col-md-offset-4 col-sm-offset-2 col-sm-8">
<div class="panel panel-default">
<div class="panel-heading"><h2 class="panel-title"><i class="fa fa-key"></i> <?php echo $text_heading; ?></h2></div>
<div class="panel-body">
<?php if ($error_warning) { ?>
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> <?php echo $error_warning; ?>
<button type="button" class="close" data-dismiss="alert">×</button>
</div>
<?php } ?>
<?php echo form_open('login');?>
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i> </span>
<input type="text" name="username" value="" placeholder="Username" class="form-control" size="50" />
</div>
<?php echo form_error('username', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-lock"></i></span>
<input type="password" name="password" value="" placeholder="Password" class="form-control"/>
</div>
<?php echo form_error('password', '<div class="text-danger">', '</div>'); ?>
</div>
<div class="form-group">
<div class="text-right">
<button type="submit" class="btn btn-primary"><i class="fa fa-key"></i> Login</button>
</div>
</div>
</form>
</div><!--/. Panel Body -->
</div><!--/. Panel Panel Default -->
</div>
</div>
</div>
<?php echo modules::run('common/footer/index');?>
答案 0 :(得分:1)
set_flshdat是codeigniter函数,只能用于下一个服务器请求,然后自动清除。您可以在此处详细查看flashdata并在此链接中搜索set_flashdata
所以如果您在重定向到其他页面或同一页面后需要消息
在第一个请求的控制器页面上设置flashdata
$this->session->set_flashdata('message_name', 'This is my message');
并在第二个服务器请求或您的重定向页面将使用下面的
获取此Flash数据echo $this->session->flashdata('message_name');
所以,基本上如果你不想重定向页面并想加载视图
//set data in controller
$data['message_name'] = 'This is my message'
//and pass this to view
$this->load->view('yourview_name', $data);
在你的视图页面中写下
echo $message_name;