CodeIgniter Forums
Session disappears and re-appears? - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Session disappears and re-appears? (/thread-44205.html)



Session disappears and re-appears? - El Forum - 08-07-2011

[eluser]SitesByJoe[/eluser]
Okay, I'm scratching my head over this one so maybe some of you guys have some advice.

I built a site http://www.collegescholarships.com and I use Sessions and Erkanaauth.

When a user logs in 2 things should happen:

1. The top of the pages give a "Hi There {username)" messages and,
2. The link in the navigation that says "Find Scholarships" changes to "My Scholarships".

I thought everything was working fine, but the weirdest thing happens:

On a couple pages on the site, my session disappears. If I go to a different page it comes back!

So digging in a little further, here's the view that shows the account message:

Code:
<?php if ($this->erkanaauth->try_profile_session_login()) : ?>
    
  <em>Hi there, &lt;?php echo $this->session->userdata('first_name'); ?&gt;!</em>
  &lt;?php echo anchor('profiles/logout', 'Logout', array('class' => 'log_in_out')); ?&gt;    

&lt;?php else : ?&gt;
    
  <em>Already have an account?</em>
  &lt;?php echo anchor('profiles/login', 'Login', array('class' => 'log_in_out')); ?&gt;
    
&lt;?php endif; ?&gt;

And the navigation does the same:
Code:
&lt;?php if ( ! $this->erkanaauth->try_profile_session_login()) : // not logged in ?&gt;
  <li id="scholarships_link">&lt;?php echo anchor('scholarships/search', 'Find a Scholarship' . is_active_link($active_link, 'scholarships_link')); ?&gt;</li>
&lt;?php else : ?&gt;
  <li id="dashboard_link">&lt;?php echo anchor('profiles', 'My Scholarships' . is_active_link($active_link, 'dashboard_link')); ?&gt;</li>
&lt;?php endif; ?&gt;

We trace that back to the Erkanaauth library, where I have this function:

Code:
function try_profile_session_login()
{
  if ($this->CI->session->userdata('profile_id'))
  {
    $query = $this->CI->db->query('SELECT COUNT(*) AS total FROM profiles WHERE id = ' . $this->CI->session->userdata('profile_id'));
    $row = $query->row();
    if ($row->total != 1)
    {
      // Bad session - kill it
      $this->profile_logout();
      return FALSE;
    }
    else
    {
      return TRUE;
    }
  }
  else
  {
    return FALSE;
  }
}

The logout function does this:

Code:
function profile_logout()
{
  $this->CI->session->set_userdata(array('profile_id' => FALSE));
}

I don't think the user is logged out since going to a new page brings the session back.

What do you think is going wrong?

It's only the homepage and maybe 3 other pages that do this. Too weird!

Here's my home/index function (one of the bad pages). You can see I'm not touching the session.

Code:
function index()
{
  // load the homepage
  $this->load->model('Pages_model');
  $data['pages'] = $this->Pages_model->get_page_by_slug('homepage');

  // use our page data to populate the meta tags
  $page = $data['pages']->row();
  $data['title'] = $page->title;
  $data['description'] = $page->description;
  $data['keywords'] = $page->keywords;

  // if we're using an auto page navigation
  $data['nav_links'] = $this->Pages_model->get_pages_for_nav();

  // add in extra stuff for more keywords etc
  $this->load->model('News_model');
  $data['widget_award_of_the_month'] = $this->News_model->get_all_news(FALSE, 'award of the month');
  $data['widget_news'] = $this->News_model->get_all_news(FALSE, 'news');
  // asides
  $data['asides'] = $this->Pages_model->get_all_asides();
  $data['categories'] = $this->Pages_model->get_all_categories();                
  $data['category_pages'] = $this->Pages_model->get_category_pages();
  $data['sidebar_pages'] = $this->Pages_model->get_sidebar_pages();

  // set our content type
  $data['content_type'] = 'dynamic';
    
  // build and send                    
  $this->load->view('templates/homepage', $data);

  // cache our homepage for faster delivery
  $this->output->cache(60);
}



Session disappears and re-appears? - El Forum - 08-07-2011

[eluser]scottwire[/eluser]
Perhaps it has to do with you caching the page?

I would start by commenting out the caching or deleting the cache and refreshing the page after you log in.

If you're not already, your fix would be to clear cache when logging in as well as after logging out.


Session disappears and re-appears? - El Forum - 08-07-2011

[eluser]SitesByJoe[/eluser]
Oh my god - I can't believe I missed that!