PhpRiot
Become Zend Certified

Prepare for the ZCE exam using our quizzes (web or iPad/iPhone). More info...


When you're ready get 7.5% off your exam voucher using voucher CJQNOV23 at the Zend Store

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 Zend_Service_WindowsAzure_SessionHandler class, which uses Windows Azure Table Storage as a session handler for PHP applications.

To use the Zend_Service_WindowsAzure_SessionHandler session handler, it should be registered as the default session handler for your PHP application:

Example 926. Registering table storage session handler

<?php
$storageClient 
= new Zend_Service_WindowsAzure_Storage_Table(
    
'table.core.windows.net''myaccount''myauthkey'
);

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

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

After registration of the Zend_Service_WindowsAzure_SessionHandler session handler, sessions can be started and used in the same way as a normal PHP session:

Example 927. Using table storage session handler

<?php
$storageClient 
= new Zend_Service_WindowsAzure_Storage_Table(
    
'table.core.windows.net''myaccount''myauthkey'
);

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

session_start();

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

// ...

Warning

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

Zend Framework