Viewed   136 times

I have an application built using CodeIgniter 3.1.6. I was carrying out testing on a subdomain on the production server. I pointed the main domain to the folder and also changed $base_url in config.php to the correct URL. ($cookie_domain within config.php has never been set.)

However, session data is now not working. I have tried some testing, the session data can be set and read within one controller.

$this->session->set_userdata('name', $name);
echo $this->session->userdata('name');

However this doesn't work across URLs. For example:

// controllers/Contact.php
$this->session->set_userdata('name', $name); 

// controllers/Welcome.php
echo $this->session->userdata('name');

Any ideas as to why this may not work on a different domain?

 Answers

4

There have been several issues reported for incompatibility of PHP version 7.1 and CI 3.1.6 not supporting $this->session->set_userdata('name', $name);

well, $this->session->set_userdata('name', $name); works, but the function userdata() accepts only one argument and expects it to be a string

if you look into session library (/system/libraries/Session/Session.php), you'll find near row

747:

/**
 * Userdata (fetch)
 *
 * Legacy CI_Session compatibility method
 *
 * @param   string  $key    Session data key
 * @return  mixed   Session data value or NULL if not found
 */
public function userdata($key = NULL)
{
    if (isset($key))
    {
        return isset($_SESSION[$key]) ? $_SESSION[$key] : NULL;
    }
    elseif (empty($_SESSION))
    {
        return array();
    }

    $userdata = array();
    $_exclude = array_merge(
        array('__ci_vars'),
        $this->get_flash_keys(),
        $this->get_temp_keys()
    );

    foreach (array_keys($_SESSION) as $key)
    {
        if ( ! in_array($key, $_exclude, TRUE))
        {
            $userdata[$key] = $_SESSION[$key];
        }
    }

    return $userdata;
}

but alternatively you can fetch an array like $name=array('firstname'=>'mr smith') with native $_SESSION like this:

set:

$_SESSION['name']=$name; 

or

$this->session->set_userdata('name', $name);

get:

echo $_SESSION['name']['firstname']; //etc..
Monday, August 29, 2022
4

This was driving me crazy for hours but the solution (as usual) was simple. I had forgotten to make sure

$config[‘encryption_key’]

was the same for both applications!

Working great now

Wednesday, August 3, 2022
 
2

Edit your user_agent.php in /config, and add the bots you see in your session, adding them to the bot section should eliminate the sessions from logging.

// There are hundreds of bots but these are the most common.
$robots = array(
    'googlebot'     => 'Googlebot',
    'msnbot'        => 'MSNBot',
    'baiduspider'   => 'Baiduspider',
    'bingbot'       => 'Bing',
    'slurp'         => 'Inktomi Slurp',
    'yahoo'         => 'Yahoo',
    'askjeeves'     => 'AskJeeves',
    'fastcrawler'   => 'FastCrawler',
    'infoseek'      => 'InfoSeek Robot 1.0',
    'lycos'         => 'Lycos',
    'yandex'        => 'YandexBot'
);

You can reduce the # of bots, but won't eliminate them. This user-agent process could be used to create a MY_session.php and then exclude session creation for agents matching bots.

EDIT:
I went ahead and created this on github and documented here:
http://blog.biernacki.ca/2014/01/codeigniter-keeping-bots-out-of-your-sessions-table-or-how-i-cleaned-up-my-sessions/

Enjoy

Monday, December 5, 2022
2

The most common solution to this problem is usually the missing question mark ? after index.php in .htaccess, so

RewriteRule ^(.*)$ index.php/$1 [L]

should be

RewriteRule ^(.*)$ index.php?/$1 [L]

E.G. on my Windows XAMPP, I do not need the "?", but in a Linux hosting environment it is usually required.

Wednesday, December 21, 2022
 
5

I solved my problem.There was some issue with ie and chrome cookie management.

I change the 'sess_cookie_name' in config from 'ci_session' to 'cisession'

and sess_expiration from 7200 to 84200 and it got fixed.

Tuesday, August 23, 2022
 
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :