Welcome Guest, Not a member yet? Register   Sign In
[CLOSED] Codeigniter Sessions Redirect If Expired
#1

[eluser]riwakawd[/eluser]
I have sessions working and also CSRF for codeigniter. But I would like to know what is the best method of redirecting sessions if expired to my login page and have a error show up saying "Sessions have expired please login"

This is how I display my errors

Code:
if (array_key_exists('warning', $this->error)) {
   $data['error_warning'] = $this->error['warning'];
} else {
   $data['error_warning'] = '';
}

I have been working on my controller but session expire redirect not working

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Controller extends MX_Controller {}

class Controller extends MY_Controller {

private $error = array();

public function __construct() {

  if($this->session->sess_expiration()) {
    if (array_key_exists('warning', $this->error)) {
            $data['error_warning'] = "Session Has Expired Please Login";
         } else {
            $data['error_warning'] = '';
         }

         redirect('login');
  }

}
}

Login View

Code:
<?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> &lt;?php echo $text_heading; ?&gt;</h2></div>
<div class="panel-body">
&lt;?php if ($error_warning) { ?&gt;
<div class="alert alert-danger"><i class="fa fa-exclamation-circle"></i> &lt;?php echo $error_warning; ?&gt;
<button type="button" class="close" data-dismiss="alert">&times;</button>
</div>
&lt;?php } ?&gt;

&lt;?php echo form_open('login');?&gt;
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-user"></i> </span>
&lt;input type="text" name="username" value="" placeholder="Username"  class="form-control" size="50" /&gt;
</div>
&lt;?php echo form_error('username', '<div class="text-danger">', '</div>'); ?&gt;
</div>
<div class="form-group">
<div class="input-group"><span class="input-group-addon"><i class="fa fa-lock"></i></span>
&lt;input type="password" name="password" value="" placeholder="Password"  class="form-control"/&gt;
</div>
&lt;?php echo form_error('password', '<div class="text-danger">', '</div>'); ?&gt;
</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>
&lt;/form&gt;
</div>&lt;!--/. Panel Body --&gt;
</div>&lt;!--/. Panel Panel Default --&gt;
</div>
</div>
</div>
&lt;?php echo modules::run('common/footer/index');?&gt;
#2

[eluser]CroNiX[/eluser]
What is $this->session->sess_expiration() ? That function is not part of the session library. It's a property, so you should use it without parentheses. But I'm still not sure what you're doing there. You're basically saying:
Code:
if (7200) {}
7200 being the (default) number of seconds the session should last.
#3

[eluser]CroNiX[/eluser]
The easiest way to do this is to just set a session variable after they log in and then test for it.

After successful login:
Code:
$this->session->set_userdata('logged_in', TRUE);

If the session expires, or they log out (and you remove their session data when they do), this variable will no longer be set in session so you can test for it. If a session variable does NOT exist, the session class will return boolean FALSE.

Code:
if ($this->session->userdata('logged_in') === FALSE)
{
  redirect('login');
}




Theme © iAndrew 2016 - Forum software by © MyBB