PHP Classes
elePHPant
Icontem

File: examples/nonblock.php

Recommend this page to a friend!
  Classes of Colin McKinnon  >  Stackable PHP Session Handler  >  examples/nonblock.php  >  Download  
File: examples/nonblock.php
Role: Example script
Content type: text/plain
Description: Using the nonblocking handler
Class: Stackable PHP Session Handler
Store session data with multiple session handlers
Author: By
Last change:
Date: 1 year ago
Size: 1,456 bytes
 

Contents

Class file image Download
<?php
require_once('utils.inc.php');
require_once(
'../handlers/stackSess.inc.php');
require_once(
'../handlers/nonBlocking.inc.php');
session_name('SESS_NB');

logger("started");
$handler=new nonBlockingHandler();
$handler->setLogger('logger');

if (!
$handler->install()) {
    print
"set handler failed";
    exit;
}
logger("* about to call session_start()");

session_start();
if (!isset(
$_SESSION['c'])) $_SESSION['c']=0;
if ((integer)
$_SESSION['c'] && !($_SESSION['c'] % 3)) {
   
logger("* about to regenerate");
   
session_regenerate_id();
        if (
version_compare(PHP_VERSION, '5.5.1') < 0) {
               
$handler->create_sid('dummy string');
        }

}
++
$_SESSION['c'];
logger("about to finish");
session_write_close();
?>
<html>
<H1>The Non-Blocking Handler</H1>
<p>
This handler behaves in a similar way to the builtin session handler - i.e.
it is a storage handler, using files (and optionally a hierarchy of directories).
</p><p>
The difference is that the session is not locked while open. Clearly this
can have unexpected consequences if more than one script writes the file.
The write method checks to see if the files timestamp has changed before issuing a write, and triggers an error if this has occurred.
</p><p>
Dangers aside, this handler can be faster than even the builtin
handler.
</p><p>
The logging output of the handler is shown below:<br />
<?php
print "OK:++\$_SESSION['c']=" . $_SESSION['c'] . "<pre>$statuslog</pre>";
exit;