X
X

Session start options

When you want to start a session we use the session start function which now accepts an associative array as the first parameter. The key is the option you would like to set and the value of the key is what you want to set the option to.

To find the session settings start XAMPP, boot up Apache and finally go to your web browser and type http://localhost/phpinfo.php this’ll provide a page with all the PHP configurations. If you press command or control + F and then search for session, keep searching and you’ll see the heading ‘session settings’. Under this heading is all the available options you can configure by default for your sessions.

We can now see all the directives and pick one, for example the directive session.cache_expire is 180 but lets say I want longer than this for my cache to expire for a particular session. So for example…

session_start( [
	cache_expire => 280
] );

This will set up this session to have a cache expiration of 280 and not 180 and I didn’t have to change this option by reconfiguring the server settings.

Also there’s a new read_close option that accepts either true or false. If you wanted to turn this on and have your cache set to 280 here’s the code to do it…

session_start( [
	read_close => true,
	cache_expire => 280
] );

This will now setup a session with the new read_close option that can only be set by session_start, the cache for this session will be 280 instead of 180. But what does the read_close option do? This option allows data from the session to be read and then immediately be deleted to prevent locking or taking up unnecessary space on the server freeing up memory when needed for other session data.

So in PHP7 and above you can start a session and configure it by passing in an associative array with the keys as options and the values configure those options.

Latest Posts