Zebra_Session implements session locking. Session locking is a way to ensure that data is correctly handled
in a scenario with multiple concurrent AJAX requests. Read more about it in this excellent article by Andy Bakun
called {@link http://thwartedefforts.org/2006/11/11/race-conditions-with-ajax-and-php-sessions/ Race Conditions with Ajax and PHP Sessions}.
This library is also a solution for applications that are scaled across multiple web servers (using a
load balancer or a round-robin DNS) and where the user's session data needs to be available. Storing sessions in a
database makes them available to all of the servers!
Zebra_Session supports "flashdata" - session variable which will only be available for the next server request, and
which will be automatically deleted afterwards. Typically used for informational or status messages (for example:
"data has been successfully updated").
Zebra_Session is was inspired by John Herren's code from the {@link http://devzone.zend.com/413/trick-out-your-session-handler/ Trick out your session handler}
article and {@link http://shiflett.org/articles/the-truth-about-sessions Chris Shiflett}'s articles about PHP sessions.
The code is heavily commented and generates no warnings/errors/notices when PHP's error reporting level is set to
E_ALL.
Visit {@link http://stefangabos.ro/php-libraries/zebra-session/} for more information.
For more resources visit {@link http://stefangabos.ro/}
// from the 'install' folder! // change the values to match the setting of your MySQL database $host = ''; $username = ''; $password = ''; // this is the name of the database where you created the table used by this class $database = 'salesreport'; // try to connect to the MySQL server $link = mysqli_connect($host, $username, $password, $database) or die('Could not connect to database!'); // include the Zebra_Session class require '../Zebra_Session.php'; // instantiate the class // note that you don't need to call the session_start() function // as it is called automatically when the object is instantiated // also note that we're passing the database connection link as the first argument $session = new Zebra_Session($link, 'sEcUr1tY_c0dE'); // current session settings print_r('<pre><strong>Current session settings:</strong><br><br>'); print_r($session->get_settings()); print_r('</pre>'); // from now on, use sessions as you would normally // the only difference is that session data is no longer saved on the server // but in your database print_r(' The first time you run the script there should be an empty array (as there\'s nothing in the $_SESSION array)<br> After you press "refresh" on your browser, you will se the values that were written in the $_SESSION array<br> '); print_r('<pre>'); print_r($_SESSION); print_r('</pre>'); // add some values to the session
/** * logout by destroying the session */ public static function logout() { if (!self::$testMode) { // This is handled by cascading delete in database schema // This way if the GC deletes the session the event subscriptions will // be cleaned up // $userId = self::getUserId(); // if (isset($userId)) // { // // Unsubscribe from all events // $pdo = new EventServicePDO(); // $pdo->unsubscribeForUser($userId); // } /* * NO_ZEBRA_SESSION $_SESSION = array(); // destroy all of the session * variables session_destroy(); */ $link = mysqliConnect(); $session = new Zebra_Session($link, SESSION_HASH, SESSION_LIFETIME_SECONDS); $session->stop(); self::$xactSession = NULL; } }