0

I'm using Codeigniter with Tank Auth authentication library. When a user reaches a page that requires a login and he has not logged in, he will be redirected to the login page.

Problem: On localhost, very frequently when I refresh the webpage, it can take up up to 30 sec before the page begins to load! Sometimes, after waiting for the page to load, it appears that I've got logged out as the page that loads is the login page which I have been redirected to. On the live server, the long delay before page load does not occur, but the user can get logged out seemingly randomly.

What's happening here? How can I find out which part of the code is causing the long load times? And what's causing the random logouts?

PHP Code (Controller)

function index() {
    $this->load->module('auth');
    if(!$this->auth->tank_auth->is_logged_in()) {
         redirect('login');
    } 
// If user registered/logged in after landing on another page
    if($this->session->userdata('entry_url')) {
        $entry_url = $this->session->userdata('entry_url');
        $this->session->unset_userdata('entry_url');
        redirect($entry_url);   // redirect back to entry url before registration/login
    }

    // Set User Session Variables
    $user_id = $this->session->userdata('user_id');
    $this->load->model('main_model');
    $name = $this->main_model->get_user_first_last_name($user_id);
    $this->session->set_userdata('first_name', $name['first_name']);
    $this->session->set_userdata('last_name', $name['last_name']);

    $this->load->view('main');

config.php

$config['sess_cookie_name']     = 'cisession';
$config['sess_expiration']      = 0;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$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']  = 300;

$config['cookie_prefix']    = "";
$config['cookie_domain']    = "";
$config['cookie_path']      = "/";
$config['cookie_secure']    = FALSE;
Nyxynyx
  • 61,411
  • 155
  • 482
  • 830
  • I often see this behaviour in colleagues code, the solution is often not to use PHP's sessions at all (as they lock the session file for the entire run-time of the script), rather to use code which reads from memcache at the start, then writes to memcache at the end, with no locking in between. – David-SkyMesh Apr 06 '12 at 03:57
  • possible duplicate of [Simplest way to profile a PHP script](http://stackoverflow.com/questions/21133/simplest-way-to-profile-a-php-script) – Amber Apr 06 '12 at 04:53

2 Answers2

0

Both of your questions are basically asking us to tell you How to debug code... If you need to see the timing of your scripts, throw in some microtime() calls and output the differences to get a fairly accurate execution time.

As for the random logouts, there's to really any way for anyone to help without seeing the source code... You should narrow down the scope to something that is more than just "what could be causing this." Anything could be causing it, we have no way of offering relevant insight without having more information.

Also, what have you tried?

orourkek
  • 2,091
  • 15
  • 22
0

use the profile and benchmark class to profile your code. http://codeigniter.com/user_guide/libraries/benchmark.html

Also, check the size of the session cookie if you have one. Maybe it exceeds the allowed limit (4096), that can be a cause of logging the user out.

Vlad Balmos
  • 3,372
  • 19
  • 34