Pages

Showing posts with label storage. Show all posts
Showing posts with label storage. Show all posts

Thursday, September 12, 2013

How are sessions stored?

The default behaviour for session storage is to save the session data in a file. This behaviour can be altered by changing the session.save_handler in your php.ini file. Options can be:

1. files
2. mm
3. database
4. SQLite

If we choose we can have this stored in one of the options above.

In files  session's are stored in /var/lib/php5 in Debian based linux
file is named like "sess_v93e2jhpjho4s4ea7r93vkbvu7" and data is stored like
foo|s:3:"bar";animals|a:8:{i:0;s:3:"cat";i:1;s:3:"dog";i:2;s:5:"mouse";i:3;s:4:"bird";i:4;s:9:"crocodile";i:5;s:6:"wombat";i:6;s:5:"koala";i:7;s:8:"kangaroo";}

The mm option saves the session data into memory, this also gives significant speed increase and is often recommended by tutorials for fine tuning PHP and apache web server.

Sessions may also be stored in a database. This option provides for greater manageability of sessions and allows the programmer to perform tasks such as counting of active sessions etc.In this session storage is permanent.

With the advent of PHP5, we now have SQLite bundled with PHP. If PHP is configured --with-sqlite, you will have access to saving sessions with a PHP native database, although SQLite is not truly a database, but a file abstraction layer with and SQL interface.

php-How do sessions work?

Sessions can be used in two ways:

1. cookie based
2. url based

Most sessions are cookie based. What this means is that when a session is started, a cookie is set on the clients machine with a unique session ID or SID. The session variables are stored typically on a file on the server that matches the unique session ID. When a variable is required, the client or browser looks for the file matching the session ID and retrieves the corresponding variables from it. A typical session file stored in the default session file directory would look like this
sess_fd51ab4d1820aa6ea27a01d439fe9959

PHP-Path to Session Storage

We can get and set session storage path using the following keyword in core php

session_save_path ([ string $path ] );