CodeIgniter Forums

Full Version: Codeigniter 3 cannor remember session
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Suddenly my codeigniter applications can't remember any session after page redirect/refresh. It works perfectly on the localhost, but the problem occurs only in hosting server. I tried to deploy them to different hosting server, but no luck.

Anyone know the problem/solution?
It sounds like it maybe the sess_save_path is incorrect, I would check that first.

I set the session path to APPPATH.'sessions', which refers to sessions folder I created inside Application folder. There are bunch of files created by ci_session, which means session path should be working properly.

Here is the flow:
1. I clicked Login button.
2. Ajax sent by login page to Account/login.
3. In account/login, validate the ID and password
4. If both match, then set session data. In this case, I'm using $this->session->set_userdata($data);. Note that $data parameter I passed is a common one-dimensional array.
5. Return json to login page
6. Login page receives and processes it.
7. If the login succeeded, then redirect using javascript location object. Else display notif that the login failed.

The code should be working, because it works perfectly in local server (both pc and mobile browser) and production server (in mobile browser).

I expect that after I clicked Login button, the page should be redirected to homepage, and the sessions are still exist in my app.

But what I get in production server: after I clicked Login button, the page redirected to login page again. Which means the sessions immediately destroyed after the page redirected to somewhere. I proved it by printing $_SESSION.

If you are telling me that my computer has a problem, then you are wrong. I tested it with some computers, different networks.
Please show all your session and cookie related $config settings. Perhaps someone will see a problem.

If the problem isn't 'sess_save_path' as @insiteFX suggests, then look carefully at cookie related $config values. Because sessions worked on localhost but won't on a production server the first cookie configs to examine and experiment with are 'cookie_path' and 'cookie_domain'.

To keep the testing as simple as possible I'd suggest trying the simple controller & view at this GitHub Repo.  Using those files removes the possibility that coding logic is the problem. Instead the testing remains focused on session configuration.
Here are the configs:
$config['sess_driver'] = 'files';
$config['sess_cookie_name'] = 'ci_session';
$config['sess_expiration'] = 12 * 3600;
$config['sess_save_path'] = APPPATH.'/session';
$config['sess_match_ip'] = FALSE;
$config['sess_time_to_update'] = 12 * 3600;
$config['sess_regenerate_destroy'] = FALSE;

$config['cookie_prefix'] = '';
$config['cookie_domain'] = '';
$config['cookie_path'] = '/';
$config['cookie_secure'] = FALSE;
$config['cookie_httponly'] = FALSE;

I tried to change the values of sess_expiration, sess_time_to_update, sess_match_ip, and sess_ress_regenerate_destroy, cookie_path and cookie_domain.
But still no luck.

The config was working before, without any changes, in cookie_* or session_*. I don't know what the cause is.
Do you have any clues on this problem? Because I never encountered this problem, even since I used CodeIgniter for the first time 2 years ago.

I also wondering, why does it only work in mobile browser, not pc browser. I don't write any logic related to session writing other than login and logout or any logic that checking what kind of browser you are using.
Now I found that CodeIgniter is generating session id 3 times once a page load. I don't know what the cause is, but it does remember session. The remembered session just got replaced by those 2 new generated ids.

But I still wondering, why does it generate 3 times when opened in pc, but generate only 1 time when opened in mobile.

I tested it in pc with Google Chrome, IE11, Mozilla Firefox, and Vivaldi, but none of them works as expected. So browser compatibility is not the problem.

When it accessed with mobile, it will include __ci_last_regenerate in the session file. But if it accessed by pc, it WILL NOT include the __ci_last_regenerate in the session file. I think, this is the cause why the session regenerated multiple times. But still have no idea what causes the __ci_last_regenerate didn't stored in the session file.
Your cookie domain are weird. I get a cookie with the following domain on PC (Chrome)

Try setting it to the following
$config['cookie_domain'] = '';
Still no luck. I already set it, but codeigniter still generates 3 session ids when the page load / redirected to somewhere else.
PHP Code:
// this is wrong!
$config['sess_save_path'] = APPPATH.'/session';

// should be
$config['sess_save_path'] = APPPATH.'session'

Try that.
Thank you for help, but I finally figured it. It comes up with the server problem. It cannot remember my sessions.
To proof this, I built a simple login page using vanilla PHP, with a username field and a button.

Here is my code:
PHP Code:

if (isset(
   $_SESSION['username'] = $_POST['username'];

if (isset(
$_SESSION['username'])): ?>
    <p>You already login, <?php echo $_SESSION['username'?></p>
<?php else: ?>
    <form action="./" method="post">
        <input type="text" name="username" placeholder="Username" />
        <input type="submit" name="submit" value="LOGIN" />
<?php endif ?>

the code above should be working, but when I refresh the page (not with F5, but instead, I click the address bar and press enter) the session is suddenly forgotten.

Then I try another server (the free one). I uploaded my code there, set everything up (I don't set up the session or cookie ones), and voila! My app working as charm.

But really, I don't know what causing this. But I suspect some things (I don't have any strong proof about this, so I will investigate deeper and let you guys know once I catch the culprit).