Tuesday, July 29, 2008

Application Variables in PHP

If you have been doing development using ASP, you must be familiar with the Application variables. 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
echo $_APP["visitor_count"]++;

In fact, application variables have many other uses as well than what has been depicted in this article.


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();

//You are visitor number
echo $_APP["visitor_count"]++;

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.

0 comments: