How to use PHP native SESSIONS in Slim


#1

Hello All,

I have built a cordova mobile application that uses slim framwork to create API’s to communciate with my database.

The last step before I finish the project is properly setting up SESSIONS so that a users data is transfered between pages.

At the moment, I have manually set up the session id so it creates one SESSION file in the tmp directory to be accessed. It works fine however this isn’t ideal as i wish to have multiple users accessing the app at one time.

Now I’m very unfamilair with how both PHP sessions work and how Slim uses SESSIONS so any help given needs to be in laymans terms.

I have heard about using middleware but again I’m not sure how to use or even install it. Essentially, i just want to be able to carry on using the PHP native SESSIONs i have in place but allow for multiple users to have their own SESSION file.

Currently, I have session_id('TEST) and session_start() in every API function thats being called and this works fine. If i were to take away the session_id and let the application creates sessions for me, every time a API is called, a new session file is being created with a long file name.

Here is my setup and an example of my issue:

Capture

Above is what happens when I remove the manual session_id('TEST)

jJeEd

It may be something very simple for you guys so I’m sorry. This is the last step of my work so any assistance with this will be greatly appreciated.

Many Thanks


#2

Slim itself doesn’t do anything with sessions. That is left up to you. If your entire application will use sessions (or most of it) then you might want to put your session_start() call in your front controller (index.php).

I know nothing about building Cordova apps, but as far as PHP and sessions go, let PHP handle naming the session name for each user rather than setting it to ‘TEST’ as you are essentially giving every user that session.

To learn about sessions, I’d have a look at these resources.

https://www.w3schools.com/php/php_sessions.asp

https://secure.php.net/manual/en/book.session.php