PHP Development

Zend Framework - Sessions in Memcache

Lets start with, this is working for me and I am posting this because of the time it took me to find the little information I could about Zend_Session::setSaveHandler utilizing memcache.

We are going to make the assumption you have memcache loaded and working; cause I don’t feel like explaining the howto install memcache. We are also going to assume you know a bit about ZF, mainly because I suck at explaining the is, why, and where of programming.

  1. Create a customer session handler that implements Zend_Session_SaveHandler_Interface
  2. In your boot strap load your session handler class, Zend_Loader::loadClass(’Your_SessionHandler_Class’)
  3. In your boot strap Zend_Session::setSaveHandler(new Your_SessionHandler_Class($_cache)); .. more about the $cache var in a sec
  4. Zend_Session::start(); and your good to go

Steps By Example:

Step 1.

class Lib_Custom_Helper_SessionHandler implements Zend_Session_SaveHandler_Interface {
private $maxlifetime = 3600;
public $cache = '';
public function __construct($cacheHandler) {
$this->cache = $cacheHandler;
}
public function open($save_path, $name) {
return true;
}
public function close() {
return true;
}
public function read($id) {
if(!($data = $this->cache->load($id))) {
return '';
}
else {
return $data;
}
}
public function write($id, $sessionData) {
$this->cache->save($sessionData, $id, array(), $this->maxlifetime);
return true;
}
public function destroy($id) {
$this->cache->remove($id);
return true;
}
public function gc($notusedformemcache) {
return true;
}
}

Step 2:
Zend_Loader::loadClass('Lib_Custom_Helper_SessionHandler');

Step 3:
Ok, so after some reading I saw a few ways to plug memcache into my session handler. I opted for this option because I was already establishing config variables and connection information for Zend_Cache, utilizing memcache, so instead of passing the connection info again, I am passing my sessionhandler my cache handle which feeds directly into memcache as it is. So before my session setup I run
$_cache = Zend_Cache::factory('Core', 'Memcached', $frontendOptions, $backendOptions);
and then
Zend_Session::setSaveHandler(new Lib_Custom_Helper_SessionHandler($_cache));

Step 4:
Zend_Session::start();

And there you have it… my sessions are now stored into memcache via my already running cache handler… Hope this helps someone out there… took me forever to find info on specific howto’s…

PHP Development, Vexed Daily

White Star Relaunched

Well I found myself with some downtime recently and decided to go back to freelance development. I figured it would be best to have some sort of online front facing site. In the past I have always approached my freelance development work from a business aspect, but have decided this time to take a more individual approach.

It still has some minor work to be done, but for the most part it is complete. White Star Studios

PHP Development

Zend Framework - Mysqli - Socket Connections

I found my first real dislike / flaw in the Zend framework; it was easily fixed thanks to a write-up that I found online, but still annoying.

It appears that in the database adapter for mysqli within the ZF the use of a socket for your connection rather than directly from host was not permitted. There is an simple solution that I found at This website, and after making these adjustments to the zend library everything works fine.

The fix is as simple as adding $socket to an array that is then passed to the ZF connection protocol. I am very curious as to why the ZF team left out socket from the connection string for mysqli and not for other database adapters.

PHP Development

Zend Framework - Smarty or not to Smarty

I have immersed myself in the Zend Framework for the past 30 days; spending that time learning the ins-and-outs. I’ve started writing my first application utilizing the framework, and must say so far I am pretty happy with it’s flexibility. The learning curve is a bit higher than I had expected, but I attribute this to the vast amount of complexity involved in trying to make the application flexible.

In the development of the front end of my application I found myself at dilemma to Smarty or not to Smarty (or a derivative there of). I started researching the history of Smarty and other types of similar formated coding standards if you will, to better understand the real concept for using such a format. The classic and consistent argument is separation of application code from display code, in this instance the separation of php from html code. Which only brought forth further questions; from my perspective with a properly designed MVC application utilizing models to translate object code to visual representations, Smarty did not appear to be needed.

A quick and rough example. Your application returns you an array of data; lets say

$data = array('firstname'=>'nick', 'lastname' => 'white');

If I wanted to display the name in a table within my html, traditionally you would begin your html table tags, foreach loop through the array adding your table data tags as needed and end your php code and respectively your table.

Using what I consider to be a properly formated MVC environment, I would apply a data model within the action controller of my application, formatting the “mynametable” prior to the rendering layer and assigning the model to a variable. Using this layout within my application allows me to need no more than the standard echo variablename to display the table and it’s data.

So I guess my question is, if you utilize your model’s within your MVC environment to handle display data structure, do you really need a Smarty system to display the data….

Leave comments… I want to see your thoughts.

Next »