Reputation:
What is the best way to set flash data messages when session have timed out and then be able to get message on login page.
I have a warning variable that would like to use but can not seem to get it working with sessions timed out I have it redirecting to the login page one sessions have timed out.
But not sure best way to set flash data using my error array, any idea's.
Login Controller:
<?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;
}
}
}
Login View
<?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');?>
Upvotes: 0
Views: 4294
Reputation: 344
set_flshdat is codeigniter function that will only be available for next server request and then automatically cleared. you can see in detail here flashdata and search for set_flashdata in this link
so if you need message after redirect to other page or same page
set flashdata on first requested controller page
$this->session->set_flashdata('message_name', 'This is my message');
and on second server request or your redirection page will get this flash data using below
echo $this->session->flashdata('message_name');
So, basically if you don't want to redirect page and want to load view
//set data in controller
$data['message_name'] = 'This is my message'
//and pass this to view
$this->load->view('yourview_name', $data);
And in your view page just write
echo $message_name;
Upvotes: 1