CodeIgniter Forums
CI4 - session not persisting - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forum-28.html)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forum-30.html)
+--- Thread: CI4 - session not persisting (/thread-71961.html)

Pages: 1 2


CI4 - session not persisting - happyape - 10-18-2018

I just uploaded a test site on an ubuntu server and it seems that session data is not being persistent.

e.g. mydomain/home/test/?ds=set&key=name&val=xyz would print the $ss with the value of key and val. However, I back to mydomain /home/test without ds=set querystring - it should print the session value set in the previous request but its empty.

Code:
   $ds = $this->request->getGet('ds');
   $key = $this->request->getGet('key');
   $val = $this->request->getGet('val');
   if($ds == 'set') {
       $data[$key] = $val;
       session()->set($data);
       }
       $ss = session()->get();
   print_r($ss);

I cleared all files from writable/session folder and if I refresh site page it creates a session file in there. I also noticed that every page refresh creates a new session file in this folder. I am not sure what's going wrong here. Is this a permissions issue?
The session file (in writable/session folder) has permission 600 i.e. group has read/write.

Is that causing an issue? I am not sure. How do I fix it?


RE: CI4 - session not persisting - donpwinston - 10-18-2018

You need $ss = session()->get($key);


RE: CI4 - session not persisting - happyape - 10-18-2018

(10-18-2018, 01:16 PM)donpwinston Wrote: You need $ss = session()->get($key);

Yes if I want a single vallue then I can provide a key but session()->get() returns all the session data.


RE: CI4 - session not persisting - happyape - 10-19-2018

Any one any ideas? what could be going wrong with session?


RE: CI4 - session not persisting - sv3tli0 - 10-19-2018

session started ?


RE: CI4 - session not persisting - happyape - 10-19-2018

(10-19-2018, 06:58 AM)sv3tli0 Wrote: session started ?

Yes I have already put this in the constructor
Code:
$session  = \Config\Services::session();

On my local windows machine, everything works well (even without the above line in the constructor).


RE: CI4 - session not persisting - happyape - 10-19-2018

I have now tried Database option too but still no luck. Locally, on Windows machine, both files and database work without any issues.

I am pretty sure it's something to do with cookies settings but most of the config is what you have there by default. I haven't made any made any modifications. Also, I have got the latest code of CI4.

What's goig wrong with cookies?


RE: CI4 - session not persisting - puschie - 10-22-2018

do you use the same web server config on host/remote ? you should get the "almost" same result on any host -> so pls compare your configs and look for some uncommon errors/messages in your web server log.

sry cant help you much as long as we cant reproduce the problem.


RE: CI4 - session not persisting - happyape - 10-23-2018

(10-22-2018, 06:16 AM)puschie Wrote: do you use the same web server config on host/remote ? you should get the "almost" same result on any host -> so pls compare your configs and look for some uncommon errors/messages in your web server log.

sry cant help you much as long as we cant reproduce the problem.

Yes I understand. Everything was same except on the Ubuntu server I had php fpm. I changed that to mod_php and it seemed to have fix the issue for me. It was a very odd issue really.


RE: CI4 - session not persisting - InsiteFX - 10-23-2018

I start the sessions in a Base controller that all the other controllers extend.

In the constructor I have the sessions like so.

PHP Code:
    /**
     * __construct ()
     * -------------------------------------------------------------------
     *
     * Class    Constructor
     *
     * NOTE: Not needed if not setting values or extending a Class.
     *
     */
    
public function __construct()
    {
        
// Ensure session is started and running
        
if (session_status() == PHP_SESSION_NONE)
        {
            
// session has not started so start it
            
session()->start();
        }
    } 

I can then use the $__SESSION global any place in the app even views.