9

I have created a login system in codeigniter project, which is working fine in Firefox but doesn't work in Chrome, data fetch and set for session but when this code redirect control to admin/dashboard, there we check session data again. If session data not exist then the code will redirect user to login again............ My code is below, and i don't know what is wrong with this session/cookies in codeigniter.?

        $data = array(
             'user_id' => $user->id,
             'name' => $user->name,
             'user_type' => $user->type,
             'username' => $user->username,
             'is_logged_in' => true
        );

        $this->session->set_userdata($data);
        //echo "user ".$this->session->userdata("username"); exit;
        redirect("admin/dashboard", "location"); 

I have search alot for but no one work in this situation, like a change "cookie_domain" in config file but nothing happened....

ime.devdesks
  • 394
  • 3
  • 4
  • 13

12 Answers12

19

I have also faced this situation and fixed it by increasing session expiry to one day.

UPDATE

Why it works by extending session expiry?


This problem occurs due to difference between timezones of user and webserver location e.g. I live in Pakistan which is 10 hours ahead of US timezone and my server is in US. I request the page at 17/10/2012 14:00 at Pakistan time. The time in US is 17/10/2012 4:00 since webserver is in US and session expiry is set to 2 hours the cookie sent by server is set to expire at 17/10/2012 6:00. Now browser interacts with your local pc time and it gets time 17/10/2012 14:00 therefore it deletes the cookie or your cookie always refreshed on your request. Therefore its best to set session expiry to 1 day because the largest timezone difference is 17 hours between new zealand and US (i am not sure about difference may be i am wrong). So your cookie will at least keep alive for 7 hours

Code Prank
  • 4,209
  • 5
  • 31
  • 47
2

just to add my experience, I'm using Codeigniter 2.1.4,

Issue: sessions on Chrome didn't last more than a couple minutes, or get logged out while doing ajax requests. I tried several approachs: (changed sess_cookie_name, longer sess_expiration time).

I just found that setting sess_encrypt_cookie to FALSE fixed the chrome logout issue.

$config['sess_encrypt_cookie'] = FALSE;

Not sure why, maybe cookie length issue on chrome (as far as I know encryption increases a lot a string length).

I know encrypting adds an extra level of security, but as far as I've read, the data stored in the cookie is not sensitive.

Hope it helps!

1

I changed the sess_match_useragent param to false in the config.php

$config['sess_match_useragent'] = FALSE;

And the problem with google chrome was resolved.

mariofertc
  • 383
  • 2
  • 7
0

Did you check that you are storing session for a longer time and it is not destroyed early.Or may be it has to do with chrome settings for accepting session and cookies data as there is no such code in codeigniter which will work on Firefox and it will not work on chrome.Also make sure you are auto loading your 'session' library.

Sachin Prasad
  • 5,365
  • 12
  • 54
  • 101
0

perfect answer by Shayan Husaini I had the same problem and i resolved it by changing

$config['sess_expiration'] = 8600;

which is seconds in 24 hours, it was seconds in 2 hours

$config['sess_expiration'] = 7200;

I live in Pakistan and using server of USA so this problem pop up.

Thanks Shayan it was great help.

Community
  • 1
  • 1
Yasir
  • 191
  • 1
  • 4
0

I encountered the same problem in and finally manage to solve it by updating the sessions database table with the sql structure from the latest version of Codeigniter at this time, version 2.1.4.

    CREATE TABLE IF NOT EXISTS  `ci_sessions` (
         session_id varchar(40) DEFAULT '0' NOT NULL,
         ip_address varchar(45) DEFAULT '0' NOT NULL,
         user_agent varchar(120) NOT NULL,
         last_activity int(10) unsigned DEFAULT 0 NOT NULL,
         user_data text NOT NULL,
         PRIMARY KEY (session_id),
         KEY `last_activity_idx` (`last_activity`)
    );

Also note that if you have multiple databases configured in the config/database.php, the session table using the the session table of the default configuration's databse.

blackmambo
  • 152
  • 1
  • 2
  • 16
0

Solved, CodeIgniter has some problem if the specified domain for cookies is localhost, I've set a fake domain with a real domain's name structure into the hosts file and it works.

SoheilYou
  • 907
  • 5
  • 23
  • 43
0

I know this is a late answer but if you're looking for a good solution here it is: use this code as the logging out code:

$this->session->sess_destroy();

This will insure removal of session "ci_session" which is the main reason you can't save a new session, hope it helps

Majid K
  • 43
  • 5
0

Check your server php version if its 7.x change to 5.6 it works for me

Kunal Waghmare
  • 183
  • 3
  • 10
0

I had the same problem and I could solve the problem by changing value of $config['sess_time_to_update'] = 3; in /application/config/config.php of CodeIgniter framework.

derloopkat
  • 6,232
  • 16
  • 38
  • 45
0

I had also faced same issue. But I have fixed it by setting proper value at $config['cookie_path']. If your project run from a sub-folder of your domain. then please set sub-folder name as value of $config['cookie_path'];

Suppose your project url : https://example.com/demo1/ then $config['cookie_path'] should like $config['cookie_path'] = '/demo1/'

0

try updating to the latest version. I'm using version 3.1.9 no problem with session.

https://codeigniter.com/userguide3/installation/downloads.html

Murosadatul
  • 116
  • 6