Welcome Guest, Not a member yet? Register   Sign In
Redirect create a new session row in the database
#1

[eluser]it.peds[/eluser]
I have tried with different config, autoload or load at controller construct, and different browsers, but all have a same result, <b>
Code:
redirect('another_controller')
creates a new row with every field is same except session_id & userdata in the session table in the database</b>

I store log-in information in the userdata. I vardump
Code:
$this->session->all_userdata()
after passing the verification, and I can see the stored userdata; however, I vardump it again in the redirected controller, and all I see is a new session without any userdata and with different session_id, even the last_activity is same (should this be a timestamp?).

I am using CI 2.1.2 to develop a website that requires log-in. Please suggest either how to fix this redirect + session problem or another way to remember an user is logging in. Thank you very much.

BTW, I just realize refreshing pages also creates a new session row. And forget to mention, even with or without .htaccess to hide the index.php, same result.
#2

[eluser]alwaystyred[/eluser]
I'm having this problem as well in Codeigniter 2.1.0, I try to:
Code:
if (!$this->session->userdata('logged_in')) {
in MY_Admin_Controller and then I get this error:

Code:
A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /application/libraries/MY_Admin_Controller.php:1)

Filename: libraries/Session.php

Line Number: 416

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /application/libraries/MY_Admin_Controller.php:1)

Filename: libraries/Session.php

Line Number: 672

A PHP Error was encountered

Severity: Warning

Message: Cannot modify header information - headers already sent by (output started at /application/libraries/MY_Admin_Controller.php:1)

Filename: helpers/url_helper.php

Line Number: 565

If anyone knows how to fix this, I am desperate to know. I would upgrade to the latest version, but it doesn't look like that will help
#3

[eluser]it.peds[/eluser]
If you sure the error is cause by the line you posted,
Code:
$this->session->userdata('logged_in')
returns string, not boolean variable, even though you have set
Code:
$this->session->set_userdata(array('logged_in' => TRUE))
see system/libraries/Session.php
#4

[eluser]alwaystyred[/eluser]
I have added random echos to try and isolate the problem:
Code:
class MY_Admin_Controller extends CI_Controller {

    function __construct()
    {
        parent::__construct();
  echo '1';
  $this->view_data['base_url'] = base_url();
  echo '2';
  if (!$this->session->userdata('logged_in')) {
  echo '3';
  redirect(base_url() . 'login ');
It echos 123 and then of course won't redirect. It won't redirect without the echos either because of the error message it outputs.

I have tried it with:
Code:
if (!$this->session->userdata('logged_in')) {
Code:
if ($this->session->userdata('logged_in') == TRUE) {
Code:
if ($this->session->userdata('logged_in') === TRUE) {

But each time it fails because of the problem with the session being broken by a page refresh. The code works fine until you refresh the page (which I guess means it doesn't work fine!)
#5

[eluser]Aken[/eluser]
This is likely an issue with your sessions not sticking, not the code doing the checking. Search around for "new session every page load" or something along those lines - it comes up a lot, and is usually a cookie settings problem.

Oh, and you don't need base_url() in the redirect() function. Redirect will automatically use the site_url() function to create the URL.
#6

[eluser]alwaystyred[/eluser]
Thanks Aken, I'll have a look into this




Theme © iAndrew 2016 - Forum software by © MyBB