CodeIgniter Forums
Help..Session always needs manual refresh to show correct session. - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Help..Session always needs manual refresh to show correct session. (/showthread.php?tid=53868)



Help..Session always needs manual refresh to show correct session. - El Forum - 08-12-2012

[eluser]frochim[/eluser]
Hi all...
Currently i'm using CI 2.1.2 and store session using database. After successful login, login controller redirect to home controller and displaying another user session. If users press CTRL+F5 (manual refresh), it showing correct user session.

Autoload Libraries: Session,Database
Autoload Helper: Url

Config
Code:
$config['sess_cookie_name'] = 'cisession';
$config['sess_expiration'] = 3600;
$config['sess_expire_on_close'] = TRUE;
$config['sess_encrypt_cookie'] = TRUE;
$config['sess_use_database'] = TRUE;
$config['sess_table_name'] = 'ci_sessions';
$config['sess_match_ip'] = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update'] = 3600;

Controller: Login
Code:
class Login extends CI_Controller
{
function __construct() {
  parent::__construct();
  $this->load->helper('form');
  $this->load->model('login_model');
}
function index() {
  $this->load->view('login');
}
function check() {
  $username = $this->input->post('username');
  $password = $this->input->post('password');
  if($this->login_model->check($username,$password)) {
   $this->session->set_userdata(array(
    array(
     'username' => $username
     ,'logged_in' => TRUE
    )
   ));
   redirect('/home','refresh');
  } else {
   $this->load->view('login');
  }
}
}

Controller: Home
Code:
class Home extends CI_Controller
{
function __construct() {
  parent::__construct();
}
function index() {
  $this->load->view('home',array(
   'user' => $this->session->userdata('username')
   ,'sessid' => $this->session->userdata('session_id')
  ));
}
}

View: Home
Code:
<html>
<head><title>Welcome Home</title></head>
<body>
<?php echo "Welcome back, $user"; ?>
</body>
</html>

Any suggestion?
Thanks before,
F.Rochim


Help..Session always needs manual refresh to show correct session. - El Forum - 08-12-2012

[eluser]InsiteFX[/eluser]
For one take the forward / off of your redirect to home.



Help..Session always needs manual refresh to show correct session. - El Forum - 08-12-2012

[eluser]frochim[/eluser]
Ok, i'll try. Thanks for fast reply.


Help..Session always needs manual refresh to show correct session. - El Forum - 08-12-2012

[eluser]frochim[/eluser]
@InsiteFX: I've took off the forward / and some of users still getting wrong session. My database is MySQL and engine for ci_session is InnoDB.
Do i have to switch back to MyISAM for ci_session?


Help..Session always needs manual refresh to show correct session. - El Forum - 08-12-2012

[eluser]InsiteFX[/eluser]
The database engine should not matter.

Try turning of the webpage caching for that page.



Help..Session always needs manual refresh to show correct session. - El Forum - 08-13-2012

[eluser]frochim[/eluser]
@InsiteFX: I'm sorry, what do you mean with turning of webpage caching?
Do i have to set
Code:
$this->output->cache(5)
above
Code:
$this->load->view('home')



Help..Session always needs manual refresh to show correct session. - El Forum - 08-13-2012

[eluser]InsiteFX[/eluser]
Code:
// change to what you need.
$last_update = 3600;

$this->output->set_header('Last-Modified: '.gmdate('D, d M Y H:i:s', $last_update).' GMT');
$this->output->set_header("Cache-Control: no-store, no-cache, must-revalidate");
$this->output->set_header("Cache-Control: post-check=0, pre-check=0");
$this->output->set_header("Pragma: no-cache");

This is just to see if you are having a webpage caching problem with your sessions.



Help..Session always needs manual refresh to show correct session. - El Forum - 08-13-2012

[eluser]frochim[/eluser]
@InsiteFX: Thanks for enlightenment, i'll try and reporting the result soon.


Help..Session always needs manual refresh to show correct session. - El Forum - 08-13-2012

[eluser]frochim[/eluser]
@InsiteFX: So far no users with wrong session after successful login. Thanks.
What i've learned from this problem is everytime i want to show data from session i have to set of webpage caching to off.
This session problem is solved.