Jerahmeel Acebuche
Jerahmeel Acebuche

Reputation: 2369

codeigniter session expires after using header function

My sessions expires after using this code on my controller :

header("Location: $url?$params");

This is the main function which will make me to go to a site. After doing some process,it will redirect it back to my website.But there my session expires.what is the main problem behind this?

Here is my session settings on my config.php

$config['sess_cookie_name']     = 'ci_session';
$config['sess_expiration']      = 7200;
$config['sess_expire_on_close'] = FALSE;
$config['sess_encrypt_cookie']  = FALSE;
$config['sess_use_database']    = FALSE;
$config['sess_table_name']      = 'ci_sessions';
$config['sess_match_ip']        = FALSE;
$config['sess_match_useragent'] = TRUE;
$config['sess_time_to_update']  = 300;

Upvotes: 0

Views: 560

Answers (2)

user3492421
user3492421

Reputation:

Jerahmeel Acebuche,To my knowledge i can give you a example,if the user doesn't have cookies enabled in the first place, then cookies will be unset as the session ID carried in the URL will disappear. You need to check to see if SID is set and if so, carry it along with the URL. here; use my function instead of header():

PHP Code:

 // perform redirect- supports no cookies AND works with older browsers, too (nice!) 

  function _location($url, $params = '') { 

 if(strlen(SID)) { 

    if (strlen($params)) { 

        $params = '?'.$params.'&'.SID; 

} else { 

     $params = '?'.SID; 

    } 

} else { 

    if (strlen($params)) { 

        $params = '?'.$params; 

    } 

} 

   header("Location: ".$url.$params); 

   echo "<h3>You are using an old browser that does not support redirection. 
   Have you       thought about upgrading?<br><br> 

      <a href='".$url.$params."'>Click here to continue using the site.</a></h3>"; 


die;        // never return, no matter what 

 }  
     use it like this: _location('debug.php','flag=true') 
     --note that second argument is    optional but you can pass GEt-style variables 
       this way.to find out what I'm doing here be sure to read php.net/session

good luck,
Jerahmeel Acebuche

Upvotes: 0

Nil&#39;z
Nil&#39;z

Reputation: 7475

You need to change this config:

$config['sess_match_useragent'] = TRUE;

To:

$config['sess_match_useragent'] = FALSE;

Try this and check hopefully it will solve your problem.

Upvotes: 1

Related Questions