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 |
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
|
Usage
The usage of the $_APP variable is very similar to $_SESSION. Before using it on a page, you must include
Sample PHP file using application variables:
|
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 inapplication_start() , then compare $_APP to the original inapplication_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:
Post a Comment