PHP Classes

File: examples/example1.php

Recommend this page to a friend!
  Classes of Stefan Gabos   Zebra_Session, a wrapper for PHP’s default session handling functions, using MySQL for storage   examples/example1.php   Download  
File: examples/example1.php
Role: Example script
Content type: text/plain
Description: Example script
Class: Zebra_Session, a wrapper for PHP’s default session handling functions, using MySQL for storage
Session handler that stores session data in MySQL
Author: By
Last change:
Date: 13 years ago
Size: 2,024 bytes
 

Contents

Class file image Download
<?php

   
// first create the required mySQL table that is used by this class
    // you can do that by running in mySQL the 'session_data.sql' file
    // from the 'install' folder!
   
    // change the values to match the setting of your mySQL database
   
$MySQL_Host = '';
   
$MySQL_Username = '';
   
$MySQL_Password = '';
   
   
// this is the name of the database where you created the table used by this class
   
$MySQL_Database = 'test';
   
   
// try to connect to the MySQL server
   
$link = mysql_connect($MySQL_Host, $MySQL_Username, $MySQL_Password);

   
// if connection failed
   
if (!$link) {

       
// stop
       
die ('Could not connect to database!');

    }

   
// select the required database
   
$db = mysql_select_db($MySQL_Database, $link);

   
// if database could not be selected
   
if (!$db) {

       
// stop
       
die ('Could not select database!');

    }

   
// include the 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
   
$session = new Zebra_Session();


   
// 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
   
$_SESSION['value1'] = 'hello';
   
$_SESSION['value2'] = 'world';
   
   
// now check the table and see that there is data in it!
   
    // to completely delete a session un-comment the following line
    //$session->stop();

?>