Welcome Guest, Not a member yet? Register   Sign In
Session destroyed... Solved problem but I don't understand why
#1

[eluser]Unknown[/eluser]
Hello everyone!

I've been coding for a while with CI and I must say it's very useful and easy to use. Nevertheless, today the weirdest thing happened to me when coding some stuff with sessions and I would like to know if any of you could explain me what happened.

I have something like this in my code:

controllers/user.php
Code:
function login(){
     ...
     $data_sess = array(..., 'logged_in' => true, ...);
     $this->session->set_userdata($data_sess);
     redirect('');
     ...
}

function home(){
     if(!$this->_logged_in()){
          $this->login();
     }else{
          ...
          $this->load->view('someview');
          ...
     }
}

function _logged_in(){
     return $this->session->userdata('logged_in');
}

And the view is something like this:

someview.html
Code:
...
<img src="images/pic.png" alt="Some Img" />
...

It turns out that I log-in and then I'm redirected to index page, where I can navigate and visit any section (as logged-in user) without having any problem. That's OK. But if I go to user's home I appear as logged-in just once, this means that if I refresh the page I'm logged-out and the log-in form is showed again.
I looked for a lot of possible solutions to this "problem"... like checking the server time zone, using database for sessions, disabling sessions to match user agent, even using another session library. Finally I discovered that the image I was showing in the view was calling "images/pic.png" and that was in fact a reference to http://mypage.com/user/home/images/pic.png. Somehow, that reference was causing all the trouble. I replaced the call to the image with "&lt;?php echo base_url(); ?&gt;images/pic.png" and I solved the problem.

I don't understand why that reference was destroying my logged-in session.. I would really appreciate if someone explain that to me Smile


Regards
#2

[eluser]InsiteFX[/eluser]
Because it could not find your image!

When testing make sure you have error reporting turned on!
#3

[eluser]Unknown[/eluser]
[quote author="InsiteFX" date="1334300280"]Because it could not find your image!

When testing make sure you have error reporting turned on!
[/quote]

Thanks for replying.

I know it was because it couldn't find my image... my question was pointing to how is that related to the fact that my logged-in session was being deleted when that happened.

If I access to /user/home/blablblabla everything is OK, but if I access to user/home/bablablabla.png my session is destroyed. 404 errors has something to do with it??

Regards

#4

[eluser]Aken[/eluser]
Check if your .htaccess defaults back to your application if a 404 error occurs. If so, there's something in the logic in there that's messing with your session.




Theme © iAndrew 2016 - Forum software by © MyBB