PHP Sessions in a Load Balanced Environment

One thing that we struggle with in our server environment as many others do is doing load balancing and being able to sync session data between all the machines. Having one dedicated session space or server can be an option but that can get a little messy or expensive. So we added MySQL Sessions to the Simpl framework and it is easy as pie to use.

Originally adopted from Joseph Crawford but implemented completely in Simpl. Josephs was great except it seemed to be connected to a larger whole. The bits and pieces were there for the session but they just needed to be combined with a framework. Below is a fully featured example working and usable.

Prerequisits:

  • PHPSimpl
  • MySQL Database Connection
  • “session” Table
    CREATE TABLE `session` (
    `ses_id` varchar(32) NOT NULL,
    `last_access` int(12) unsigned NOT NULL,
    `ses_start` int(12) unsigned NOT NULL,
    `ses_value` text NOT NULL,
    PRIMARY KEY (`ses_id`)
    ) ENGINE=InnoDB DEFAULT CHARSET=latin1 COMMENT='Used to store the sessions data';

Usage Steps:

  1. Download and copy Simpl to the web server.
  2. Define some base items
    // Directories
    // Always Include trailing slash "/" in Direcories
    define('DIR_ABS', '/usr/local/www/');
    define('FS_SIMPL', DIR_ABS . 'simpl/');
    define('FS_CACHE', DIR_ABS . 'cache/');// Enable Database Sessions
    define('DB_SESSIONS', true);// Database Connection Options
    define('DB_USER', 'username');
    define('DB_HOST', 'localhost');
    define('DB_PASS', 'password');
    define('DB_DEFAULT', 'site_data');
  3. Include the framework
    // Simpl Framework
    include_once(FS_SIMPL . 'simpl.php');
  4. Connect to the database
    // Make the DB Connection
    $db = new DB;
    $db->Connect();
  5. Use the $_SESSION super global just as you normally would and PHPSimpl does all the work for you.

The only modification may be the page cannot close the database connection since the _SESSION variable has to be written after the page is completely loaded and thus must still have the connection available.

Simpl and the $db object to all the hard work of redirecting the session data and pulling it from the database. No need to call a session_start() or even interact with the session table.

If you are interested in what the Session class looks like here is the source code:
Session.php

PHPSimpl Join() jumps into action

Its amazing how much code can be reduced by using a framework, especially a framework that accomplishes all the most basic elements without interfering with usability. With PHPSimpl we are trying to accomplish just that. So we recognized the problem of information being in two different tables and having two queries just was not cutting it. So we are proud to announce the Join() function in DbTemplate. This is now integrated into the examples also to get you up and running with it.

Here is a sample code snipit of joining the Registration, User and Payments tables to view information all in one list with one query:

// Create the Registration class
$myRegistration = new Registration;
$display[] = array('registration_id','user_id','date_registered','guests');// Create the User Class
$tmpUser = new Users;
$myRegistration->Join($tmpUser, 'user_id', 'INNER');
$display[] = array('first_name','last_name');// Create the Payments Class
$tmpPayment = new Payment;
$myRegistration->Join($tmpPayment, 'conference_id', 'INNER');
$display[] = array('conference_id', 'amount', 'status', 'method');// Get a list of all the Registrations
$myRegistration->GetList($display,'date_registered','DESC');


Questions or Concerns? Head over to our PHPSimpl Group.

Just added, Output individual fields

So PHP Simpl does a good job of outputting an XHTML form with very extend able features such as labels, required *’s, size and maxlength calculation, example and error output automatically. But have you ever just wanted to output the form in your own way? Well now you can, this can be accomplished with the FormField() function inside the DbTemplate class. It handles all that same great XHTML formatting, you just have to worry about where you want to flied to be displayed.

Function Prototype:

public function FormField($field, $hidden=false, $options='', $config='');


Example:

$myPost = new Post;
$hidden = false;
$options = array();
$config = array();echo '<form>';
$myPost->FormField('title', $hidden, $options, $config);
echo '<input name="submit" value="Save" type="submit">';
echo '</form>';

PHPSimpl – An Introduction

PHPSimpl is a a PHP framework for rapidly creating classes that mirror MySQL tables. The framework comes with the basic functions to list, display and edit records from the database. With this framework a simple manager and front of a site can be created within a few minutes. An example database, manager and front end are included with the framework.

Since this is a very new framework an introduction is in order.

First, Download the framework, we host it on Google Code and you can download it via Subversion.

Second, Put it on a server that supports PHP and MySQL. It does not have to be in a web viewable directory but currently it is recommended to be in /public_html/ root.

Setup the defines. There are a few defines that are nessisary to run PHP Simpl these must be declaired before the include of the class.

define('FS_SIMPL', '/public_html/simpl/');
define('DB_USER','user');
define('DB_HOST','localhost');
define('DB_PASS','pass');
define('DB_DEFAULT','my_blog');
define('DEBUG',false);
define('DEBUG_QUERY',false);
define('USE_CACHE',true);
define('CLEAR_CACHE',false);


The first one is manditory the rest are not, but it is a good idea to define them if you do not want to go into the main PHP Simpl config to change to debug mode or to clear the cache when you want to.Next include the main class then load any helper classes you may need. This must be done before you include your own classes file.

include_once(FS_SIMPL . 'simpl.php');
$mySimpl->Load('Form');
$mySimpl->Load('Db');
$mySimpl->Load('DbTemplate');
$mySimpl->Load('Folder');
$mySimpl->Load('File');
$mySimpl->Load('Upload');
$mySimpl->Load('Image');


Now that you have PHPSIMPL up and running to write a simple class to mirror a mysql database table it is as simple as the example below.

class  Post extends DbTemplate {
function Post($data=''){
$this->required = array('title','body');
$this->table = 'blog_post';
$labels = array('is_published'=>'Published:','user_id'=>'Author:','category_id'=>'Category:');
$examples = array('category_id'=>'ex. PHP, MySQL, Cars, XML, PHPSIMPL');
$this->DbTemplate($data, $this->required, $labels, $examples, $this->table);
}
}


And you are done, this Post class now has full interaction with the database table. You can use the helper GetValue() and SetValue() functions to get and set field values and all the helper functions to interact. You can also write custom functions just like you always would if you were not using PHPSimpl. A full list of functions and their parameters will be published soon, if you cannot wait just simply look through the PHPSimpl source, everything is commented in JavaDoc format for ease of use.