Wednesday, February 4, 2009

Application Variables in PHP

In July, 2002, I moved the leosolutions.com web site from a Microsoft IIS web server to a Linux server running Apache to reduce the monthly cost of web hosting. After many years of developing in Microsoft Active Server Pages, the conversion from ASP to PHP was surprisingly easy - nearly every statment in ASP has a one-to-one correspondence with its PHP equivalent. However, I encountered one major problem during the conversion process: PHP has no couterpart for application variables in ASP.

Application variables in ASP work very similar to the $_SESSION variable in PHP. However, unlike the $_SESSION variable, application variables are not specific to an individual user; they persist across every user of every page on the web site.

Benefits

The immediate benefits of application variables may not be apparent, but esperienced ASP coders understand their power. Take this one-liner, for example:

You are visitor number

to this web site.

Application variables have many other purposes. On the leosolutions.com web site, I used them primarily to cache common dynamically-generated parts of the page, such as the menu bars and project tab. (Yes, the menu bars on my are dynamically generated from a database) Since this content doesn't change often, it would be a waste of resources (and cause slower response times) to generate these elements on-the-fly, each time a page was requested. Instead, the PHP script checks the application variables for the element. If it is found, the PHP script simply sends the HTML code stored in the application variable directly to the output. Otherwise, the content is generated from the database, and stored to the application variables for future use.

Solution

I looked on the web and read numerous other work-arounds for application variables in PHP, but found none that I liked. Every one required too much overhead code, or else multiple lines of code to actually use the variables. Here is my solution:

app.php


define("APP_DATA_FILE",
"/tmp/application.data");

function application_start ()
{
global $_APP;

// if data file exists, load application
// variables
if (file_exists(APP_DATA_FILE))
{
// read data file
$file = fopen(APP_DATA_FILE, "r");
if ($file)
{
$data = fread($file,
filesize(APP_DATA_FILE));
fclose($file);
}

// build application variables from
// data file
$_APP = unserialize($data);
}
}

function application_end ()
{
global $_APP;

// write application data to file
$data = serialize($_APP);
$file = fopen(APP_DATA_FILE, "w");
if ($file)
{
fwrite($file, $data);
fclose($file);
}
}

?>


Usage

The usage of the $_APP variable is very similar to $_SESSION. Before using it on a page, you must include app.php and call application_start(). When you are finished, you must call application_end().

Sample PHP file using application variables:

include("app.php");
application_start();
?>

< html>
< body>
You are visitor number

to this web site.
< /body>
< /html>

application_end();
?>


Optimizations and Other Notes

Numerous improvements can be made to this code, but they are not included here for simplicity.

* For added security, permissions on the application data file should not allow any user, except for the web server, to read or write to it. Otherwise, if application variables contained sensitive data, such as passwords or credit card information, other users on the system could read this data.
* To keep from writing to disk unnecessarily, only call application_end() if data in the $_APP variable has changed.
* Another alternative is to let the application_end() function determine whether the $_APP variable has been changed, and only write it to disk if necessary. Simply make a copy of the $_APP variable in application_start(), then compare $_APP to the original in application_end().
* Finally, don't forget that unlike $_SESSION, the $_APP variable is only available in the global scope. When using $_APP from inside a function, be sure to include the statement global $_APP; at the top of the function.

1 comment:

  1. Thanks a ton for this beautiful script :) it has saved a lot of my time and work.

    ReplyDelete