New session entries are being created in database even when codeigniter session library is not loaded. Why? New session entries are being created in database even when codeigniter session library is not loaded. Why? codeigniter codeigniter

New session entries are being created in database even when codeigniter session library is not loaded. Why?


For the 1st part of the problem:

As pointed out by @JamesLalor in the comments,

  1. You must either be autoloading the session

    OR

  2. You are using some external library which is, in turn, loading the session library.

For the 2nd part of the problem:

The below solution may not be the best, but it worked for me.

Multiple sessions creation problem occurs when:

  1. You have both REST Server and Client within the same CodeIgniter Application directory

    AND

  2. Session library is auto-loaded

To the Client, the user is the consumer. A session is created for the user, having the IP address of that user. A cookie is set on the user’s browser with which the session is validated and updated (or newly created) based on the validation.

To the REST Server, the Client is the consumer. Here also a session is created (if both condition 1 and 2 above is fulfilled), but this time, it is for the Client, and this session has the IP address of the server on which the Client resides (if condition 1 above is fulfilled, then it is the IP address of the same server on which your app resides). But this time a cookie could not be set, as the consumer is not a browser. Hence, the session validation fails and a new session is created each time the page loads.

Solution:

REST is stateless and every request should contain all the information required to fulfil the request. Therefore, using sessions (whose sole job is to maintain user’s state) on REST Server is considered a bad practice. It is the job of the Client to maintain the user’s session and pass the required information to the REST Server on each and every request.

Therefore, assuming that you would not be needing session within REST Server, the solution is to remove session from autoload[‘libraries’] list, within autoload.php file, and load the library within the Client constructor (or when you need it).

Sorry for grammatical errors and/or bad English. It is not my native language.