Thứ Năm, 3 tháng 1, 2013

How to use a Database for Session Handling in the Zend Framework

For many sites, session handling does not get a second thought; it happens automatically and behind the scenes with no intervention from the developer. This default method of operation is fine for most websites with low amounts of traffic. However, by default, PHP stores session information in flat files in the file system, which means that the system is not as performant as it can be, and if you have a large number of open sessions at one time, you may reach hard operating system limits on how many files can reliably exist in one directory at a time.

A better solution

Think Facebook is using flat files to store session info for the millions of users online at any given moment? Think again.
If your site is growing and slowing, improving how sessions are handled can bring back life to your site. There are several ways to do this, each with its own pros and cons, but in this article we will use a reliable, performant, and easy to setup solution that probably already exists on your server…the database.

Databases aren’t just for content

Databases can be significantly more performant than flat files, and many more sessions can be handled at one time, making them an ideal solution for sites looking to boost performance.
Fortunately, PHP provides developers with the ability to setup custom session handlers, using any type of backend. The downside is that setting this up can be a bit involved and complicated. Enter Zend_Session.

Custom session handling made easy

The Zend_Session component of the Zend Framework makes custom session handling a breeze. What’s even better, is that the Zend Framework provides a built in class for using a database as a session handler: Zend_Session_SaveHandler_DbTable. To get started, we first need to create a table to hold our sessions:

CREATE TABLE `session` (
    `id` char(32),
    `modified` int,
    `lifetime` int,
    `data` text,
    PRIMARY KEY (`id`)
);
Once your table has been created, we must make sure a database connection is available for the Zend_Session component to use. If you already have a connection to the appropriate database in your application, you can skip this step.

$db = Zend_Db::factory('Pdo_Mysql', array(
    'host'          => 'example.com',
    'username'      => 'dbuser',
    'password'      => '******',
    'dbname'        => 'dbname'));
Now that we have a database object setup, we need to setup a simple configuration for the Zend_Session_SaveHandler to use:

$config = array(
    'name'           => 'session',
    'primary'        => 'id',
    'modifiedColumn' => 'modified',
    'dataColumn'     => 'data',
    'lifetimeColumn' => 'lifetime',
    'db'             => $db
);
Note: You may also use an instance of Zend_Config as your config object if you wish

Zend_Session::setSaveHandler(new Zend_Session_SaveHandler_DbTable($config));
 
//start your session
Zend_Session::start();
Note: You MUST set up the save handler before the session begins — before calling Zend_Session::start(), or accessing any $_SESSION variables.
And that’s it! Place the above code in an early running part of your application, and any subsequent calls to Zend_Session_Namespace or $_SESSION variables directly will use your database instead of flat files. Garbage collection (the removal of old records) is done automatically for you.

Conclusion

By following these simple steps, you can improve the performance and stability of your web application by storing session data in a reliable and performant container, the database. Any database system can be used, from Mysql to Postgresql, to Sqlite, and all will increase the reliability and speed of your site.
Improving session handling with the Zend Framework is so simple that it should be one of the first things you do when creating a new site or application.

Nguon :  http://zendcoding.com/how-to-use-a-database-for-session-handling-in-the-zend-framework

Không có nhận xét nào:

Đăng nhận xét

Học lập trình web căn bản với PHP

Bài 1: Các kiến thức căn bản Part 1:  https://jimmyvan88.blogspot.com/2012/05/can-ban-lap-trinh-web-voi-php-bai-1-cac.html Part 2:  https://...