Welcome Guest, Not a member yet? Register   Sign In
strange session behaviour
#1

[eluser]cyrigniter[/eluser]
hello,

i have a site that uses the CI session library
when I log in, data is sent to the session (via set_userdata, etc.)

everything seems to be working fine when Im on the site
basically, every page checks if there is something in the userdata. if there is then user is considered logged and can access any restricted page, but if it is empty then user is redirected to the login page

If I leave the site and browse google for instance, and then try to reach a page that requires a session to be set, everything works fine
BUT if I type the login page URL, I am taken to ... the login page (this is not the expected behaviour : this page should redirect me to the index of the restricted access area, bcs I am logged in). if i reload the page, i stay on the login page.

BUT on this login page, I have a link pointing to the (same) login page. If I click this link, then my session is eventually detected and I am redirected to the index of the restricted access area.

Why is this happening ? Why isn't my session detected when I first reach the login page ? Is this a normal behaviour of the session library ?

Thanks a lot
#2

[eluser]louisl[/eluser]
Rough guess here but normally you'd disable a login check on the actual login page so it doesn't get stuck in a loop redirecting back to itself.
#3

[eluser]cyrigniter[/eluser]
actually the landing page (..com/index) IS the login page Wink
but there is no loop :
code is :
Code:
public function index()
    {
        if($this->user_model->isLogged())
            redirect('home');
        $headers['title'] = "Login";
        $this->load->view('header',$headers);
        $this->load->view('main');
        $this->load->view('footer');
    }

which seems to work fine when called from inside the website, but not from a cold URL call from another website !
#4

[eluser]louisl[/eluser]
Are you loading the session library on your other pages but not on the index page?
#5

[eluser]cyrigniter[/eluser]
well the session library is on autoload (config/autoload.php), do you think that could be an explanation ?
#6

[eluser]cyrigniter[/eluser]
silly me, I'm starting to think all this come from the redirect() function, that requires a full URL when coming from an external site
#7

[eluser]louisl[/eluser]
No it should be fine then. You might have to post up more of your code user_model, a working controller and the index page.
#8

[eluser]cyrigniter[/eluser]
you're right, I changed redirect('home') to the full URL and it behaved the same

from user_model :
Code:
function isLogged($options = array())
    {
        $id_user = $this->session->userdata('id_user');
        if(empty($id_user))
            return false;
            
        return $id_user;
    }

from home controller (works fine) :
Code:
function __construct()
    {
        parent::__construct();
        $this->load->model('user_model');
        if(!$this->user_model->isLogged()) redirect('welcome');
    }
#9

[eluser]louisl[/eluser]
OK $id_user must be empty could be 0 int '0' string null '' etc. lets see whats in session->userdata...

Comment out your redirect('home'); and change your isLogged

Code:
function isLogged($options = array()) {
        
     echo('<pre>');
           print_r($this->session->userdata);
           echo('</pre>');
          
        $id_user = $this->session->userdata('id_user');
        
        if( empty($id_user) ) {
            
            return false;
            
        } else {
            
            return $id_user;
        
        }
}
#10

[eluser]cyrigniter[/eluser]
ok now I can keep track of the session data
basically it contains my classical session information
BUT when I reach the site from outside and call the index URL (login page, my 'welcome' controller) it loads a new, empty session (a new session_id is generated)
AND when I click any link on this index page (which is the login page) or if I call another controller (for instance 'home', see above posts) then it REMEMBERS the former session and loads it (with the former values and the former session_id).

Same behavior in chrome, FF, IE8.

Could it possibly be something wrong with the cookies ?

EDIT : WOW I realized something. if I call mysite.com/welcome/ then no session is loaded (to be more precise, a new session is generated) BUT if I call WWW.mysite.com/welcome then the former session is loaded. WHY IN HELL ????

EDIT2 : same thing for every controller. CI tends to perceive an URL with www. and an URL w/o www. as different websites oO thus handling 2 different sessions in parallel. w.t.f.




Theme © iAndrew 2016 - Forum software by © MyBB