PHP Session Configuration
PHP has several configuration directives to control session handling process like session upload and URL rewriting. These are set with PHP configuration file php.ini. To know the values that are set we can use phpinfo() function. For example I get the following.
In this PHP tutorial, we are going to see about few important session configuration directives and their possible values.
PHP Session Configuration Directives
- To set possible handlers to store and retrieve session data. These are,
session.save_handler = files | mm | sqlite | user
- Default option is files to handle session data. mm makes RAM to handle session data. With sqlite option SQLite database is used. Then with user option we can have custom functions to handle the session.
- If the above session.save_handler is set as files, then we need to set the path of the directory where we want to have session data files. session.save_path directive is used to set the path.
- We can also create sub directories to let file storage module to store session files. And then, we should refer this directory path by specifying its depth. For example,
session.save_path = "N;/path"
where N is an Integer representing the depth of the sub directory.
- PHP creates session files in the above location with name as current session_id with sess_ prefix.
- We can set the session files mode by using,
session.save_path = "N;MODE;/path"
where MODE is an Octal value. The default mode is 600 (allows owner to read and write).
This directive has 1 or 0 as its value to specify whether cookies are used to store session id or not.
session.use_cookies = 0 | 1
If 1 then cookies will be used to store session id. If not, the session id is preserved by using URL rewriting.
It controls whether cookies are sent via secure connections or not. It will be set with ON | OFF values. The default is OFF.
By setting this directive cookies are used as the mandatory storage to preserve session id. It prevents session hijacking.
To specify the name of the session. PHPSESSID is the default name.
session.name = PHPSESSID
We need to start session before using PHP session function. This directive is used to start session automatically on each page request. So, we need to set session.auto_start as 1.
session.auto_start = 0 | 1
If session.auto_start is 1, managing objects into session needs further configuration. That is, auto_prepend_file directive is used to refer corresponding class.
This is used to set cookie life time. If it is set as 0, then cookie remains until browser restart.
To specify the path where the cookies will be valid.
Similarly, to specify the domain in which the cookies will be valid.
This directive is on then it will stop client side scripts to access session id preserved in cookies.
The values of these directives are used to calculate the probability to run garbage collection to clean up session data.
It has the max lifetime of the session id in seconds. If the session id reaches this limit, PHP will treat it as garbage and clear it.
This directive controls cache headers sent to the client and proxies. The possible values are,
- none – stops sending cache headers to the client.
- nocache – default option; stop client or proxy to cache page content.
- private – allows only client to cache page content.
- private_no_expire – No expire time is set with the cache header.
- public – allows both client and proxy caching.
- It specifies cache expiration time for session enabled pages in seconds.
session.cache_expire = 120
- This directive need not to be set, if session.cache_limiter = nocache.
This directive is used to preserve session id by URL rewriting. As URL are shared among multiple users, maintaining session id in URL is risky. It cause multiple access with same session_id at a time.
This directive is used to choose hash function to generate session id. 0 and 1 represents MD5 and SHA algorithms respectively.