title: Setting Flash Data When Session Expire Codeigniter
body: <p>What is the best way to set <strong>flash data messages</strong> when session have <strong>timed out</strong> and then be able to get message on login page.</p>
  
  <p>I have a warning variable that would like to use but can not seem to get it working with sessions <strong>timed out</strong> I have it redirecting to the login page one sessions have timed out.</p>
  
  <p><strong>But not sure best way to set flash data using my error array, any idea's.</strong></p>
  
  <p><strong>Login Controller:</strong></p>
  
  <pre><code>&lt;?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
  
  class Login extends MX_Controller {
  
     private $error = array();
  
     public function __construct() {
        parent::__construct();
        $this-&gt;load-&gt;library('user');
        $this-&gt;load-&gt;library('form_validation');
        $this-&gt;lang-&gt;load('common/login', 'english');
     } 
  
     public function index() {
        $this-&gt;form_validation-&gt;set_rules('username', 'Username', 'required|min_length[4]|max_length[12]');
        $this-&gt;form_validation-&gt;set_rules('password', 'Password', 'required|xss_clean');
  
        if($this-&gt;form_validation-&gt;run($this) == false) {
  
           $data['title'] = $this-&gt;lang-&gt;line('heading_title');
  
           $data['text_heading'] = $this-&gt;lang-&gt;line('text_heading');
  
           if (array_key_exists('warning', $this-&gt;error)) {
              $data['error_warning'] = $this-&gt;error['warning'];
           } else {
              $data['error_warning'] = '';
           }
  
           if (null !==($this-&gt;session-&gt;flashdata('message_name'), $this-&gt;error)) {
              $data['message'] = $this-&gt;session-&gt;set_flashdata('message_name', 'This is my message');
           } else {
              $data['message'] = '';
           }
  
           $this-&gt;load-&gt;view('common/login', $data);
  
        } else{ 
  
           if($this-&gt;validate()) {
  
               redirect('dashboard');
  
              } else {
  
                 $data['title'] = $this-&gt;lang-&gt;line('heading_title');
  
                 $data['text_heading'] = $this-&gt;lang-&gt;line('text_heading');
  
                 if (array_key_exists('warning', $this-&gt;error)) {
                    $data['error_warning'] = $this-&gt;error['warning'];
                 } else {
                    $data['error_warning'] = '';
                 }
  
                 if (null !==($this-&gt;session-&gt;flashdata('message_name'), $this-&gt;error)) {
                    $data['message'] = $this-&gt;session-&gt;set_flashdata('message_name', 'This is my message');
                 } else {
                    $data['message'] = '';
                 }
  
                 $this-&gt;load-&gt;view('common/login', $data);
  
           }
        }
     }
  
     function validate() {
        $username = $this-&gt;input-&gt;post('username');
        $password = $this-&gt;input-&gt;post('password');
  
        if ($this-&gt;user-&gt;login($username, $password)) {
            return true;
        } else {
           $this-&gt;error['warning'] = $this-&gt;lang-&gt;line('error_login');
           return !$this-&gt;error;
        }
     }
  }
  </code></pre>
  
  <p><strong>Login View</strong></p>
  
  <pre><code>&lt;?php echo modules::run('common/header/index');?&gt;
  
  &lt;div class="container"&gt;
  &lt;div class="row"&gt;
  &lt;div class="col-lg-6 col-lg-offset-3 col-md-4 col-md-offset-4 col-sm-offset-2 col-sm-8"&gt;
  &lt;div class="panel panel-default"&gt;
  &lt;div class="panel-heading"&gt;&lt;h2 class="panel-title"&gt;&lt;i class="fa fa-key"&gt;&lt;/i&gt; &lt;?php echo $text_heading; ?&gt;&lt;/h2&gt;&lt;/div&gt;
  &lt;div class="panel-body"&gt;
  
  &lt;?php if ($error_warning) { ?&gt;
      &lt;div class="alert alert-danger"&gt;&lt;i class="fa fa-exclamation-circle"&gt;&lt;/i&gt; &lt;?php echo $error_warning; ?&gt;
          &lt;button type="button" class="close" data-dismiss="alert"&gt;&amp;times;&lt;/button&gt;
      &lt;/div&gt;
  &lt;?php } ?&gt;
  
  &lt;?php echo form_open('login');?&gt;
  
  &lt;div class="form-group"&gt;
      &lt;div class="input-group"&gt;&lt;span class="input-group-addon"&gt;&lt;i class="fa fa-user"&gt;&lt;/i&gt; &lt;/span&gt;
      &lt;input type="text" name="username" value="" placeholder="Username"  class="form-control" size="50" /&gt;
      &lt;/div&gt;
      &lt;?php echo form_error('username', '&lt;div class="text-danger"&gt;', '&lt;/div&gt;'); ?&gt;
  &lt;/div&gt;
  
  &lt;div class="form-group"&gt;
      &lt;div class="input-group"&gt;&lt;span class="input-group-addon"&gt;&lt;i class="fa fa-lock"&gt;&lt;/i&gt;&lt;/span&gt;
          &lt;input type="password" name="password" value="" placeholder="Password"  class="form-control"/&gt;
          &lt;/div&gt;
          &lt;?php echo form_error('password', '&lt;div class="text-danger"&gt;', '&lt;/div&gt;'); ?&gt;
      &lt;/div&gt;
  &lt;div class="form-group"&gt;
      &lt;div class="text-right"&gt;
          &lt;button type="submit" class="btn btn-primary"&gt;&lt;i class="fa fa-key"&gt;&lt;/i&gt; Login&lt;/button&gt;
      &lt;/div&gt;
  &lt;/div&gt;
  
  &lt;/form&gt;
  
  &lt;/div&gt;&lt;!--/. Panel Body --&gt;
  &lt;/div&gt;&lt;!--/. Panel Panel Default --&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;/div&gt;
  &lt;?php echo modules::run('common/footer/index');?&gt;
  </code></pre>