Getting Started

Back to Getting Started.
Back to Table storage.

Table storage session handler

When running a PHP application on the Windows Azure platform in a load-balanced mode (running 2 Web Role instances or more), it is important that PHP session data can be shared between multiple Web Role instances. The Windows Azure SDK for PHP provides the Microsoft_WindowsAzure_SessionHandler class, which uses Windows Azure Table Storage or Blob Storage as a session handler for PHP applications.

To use the Microsoft_WindowsAzure_SessionHandler session handler, it should be registered as the default session handler for your PHP application:
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php'; // note you can also use blob storage for larger sessions

/** Microsoft_WindowsAzure_SessionHandler */
require_once 'Microsoft/WindowsAzure/SessionHandler.php';

$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');

$sessionHandler = new Microsoft_WindowsAzure_SessionHandler($storageClient , 'sessionstable');
$sessionHandler->register();

The above code registers the Microsoft_WindowsAzure_SessionHandler session handler and will store sessions in a table called "sessionstable".

After registration of the Microsoft_WindowsAzure_SessionHandler session handler, sessions can be started and used in the same way as a normal PHP session:
/** Microsoft_WindowsAzure_Storage_Table */
require_once 'Microsoft/WindowsAzure/Storage/Table.php';

/** Microsoft_WindowsAzure_SessionHandler */
require_once 'Microsoft/WindowsAzure/SessionHandler.php';

$storageClient = new Microsoft_WindowsAzure_Storage_Table('table.core.windows.net', 'myaccount', 'myauthkey');

$sessionHandler = new Microsoft_WindowsAzure_SessionHandler($storageClient , 'sessionstable');
$sessionHandler->register();

session_start();

if (!isset($_SESSION['firstVisit']))
{
    $_SESSION['firstVisit'] = time();
}

// ...

WARNING: The Microsoft_WindowsAzure_SessionHandler session handler should be registered before a call to session_start() is made!

Last edited Apr 8, 2011 at 10:41 AM by maartenba, version 6

Comments

aurelious Dec 21, 2011 at 4:03 PM 
Hi, do you have an idea why my application is 10 times slower (~400ms) with the TableSessionHandler than without it (~40ms)?

(session_start is called after the handler registration)