Improving the Speed of PHP Web Scripts
PHP web scripts lose performance for a lot of reasons. The bottleneck can be in database queries, web page access or even slow algorithms. When performance drops, the user gets frustrated waiting for results. Less users mean less business, and your web site becomes unpopular.
The main reason for performance loss is bad software analysis and engineering. Web sites often are created and launched without thorough performance testing. Databases often are designed to accommodate less data than they actually do. Algorithms often are designed poorly and usually are not optimized for speed.
When you cannot redesign the entire web design so that it runs more quickly, you must improve its performance by serving static pages instead of interpreting PHP whenever there's a hit. Let's look at the ways to achieve this goal.
The first thing that you can do is preprocess those PHP scripts, or script parts, that take more time to execute. You can do it with the help of a PHP shell. Suppose you have a web script called index.php, and you want to preprocess it. Assuming that the PHP shell is called phpsh, the command line is:
phpsh -q /some_dir/index.php > /some_dir/index.html
The file index.html is now plain HTML and doesn't need any PHP processing. You can serve it right away to the web client. But what if the PHP script results change over time? You'd have to preprocess the script every time the results are different. The solution is to preprocess the PHP script periodically.
In Linux, the easiest way to execute a given process periodically is called crontab. The following crontab entry illustrates a preprocessing that would execute every 15 minutes:
*/15 * * * * root phpsh -q /some_dir/index.php > /some_dir/index.html
However, the chosen timing might not be enough to keep the information updated. Furthermore, some scripts are not accessed over long periods of time, while others are constantly accessed, making the use of this technique pointless. In this case, a script-based mechanism is needed.
This technique preprocesses scripts periodically, but only if they are accessed. It works much like a web proxy caching system. I will show two ways of implementing this functionality: output buffering and after-time processing.
Output buffering checks the cache file date and time and only processes the script if needed. The processing is done by buffering the output and saving it in the cache file before it is sent to the client.
In PHP you do this with the help of the ob_start() function. This function will turn output buffering on and send it to a callback function. There is another function to send the buffer back to the browser: ob_end_flush(). Let's take a look at an example:
<?php
// include header file
include("header.php");
?>
<?php
// sleep for 10 seconds
sleep(10);
?>
Test:
<?php
// include footer file
include("footer.php");
?>
Inside header.php, you'll find all the cache processing. It begins
by checking whether the script needs caching by calling the
needscache() function. This function can check the need for cache
based on a time out or based on anything you like. For the purpose
of this article, the checking is based on cache time out.
If the script needs caching, the ob cycle is started, and the script output is written into the cache file. If it doesn't need caching, the script output is read from the cache file and sent to the client's browser (see Listing 1).
Listing 1. The script output is read from the cache file and sent to the client's browser.
The footer.php script simply closes the ob processing:
<?php ob_end_flush(); ?>
You can test this technique by calling the script many times before the cache times out. You'll notice that in the first call you'll have to wait ten seconds (this is because the script sleeps for ten seconds, for testing purposes), and in the following calls the output is immediate.
However, when the cache times out, you'll have to wait for the script to finish processing. Let's see how you can prevent this and give the user the illusion that the script is always fast.
After-view processing also checks the cache file date and time, but the processing is done after the file is served, solving the cache time out processing burden. This is done by caching the file after the script ends execution.
In PHP, you can do this by associating an arbitrary function with the script termination event through the register_shutdown_function(). The only file you'll have to change is header.php (see Listing 2).
Listing 2. Associating an Arbitrary Function with the Script Termination Event
I simply added the doaftercache() function that is called only after the script finishes. It then calls the script like a normal browser does and caches it. The only time you'll have to wait is when the script has never been cached before. Test it and you'll get the feeling that the script is very fast.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- New Products
- Linux Systems Administrator
- Senior Perl Developer
- Technical Support Rep
- UX Designer
- Web & UI Developer (JavaScript & j Query)
- Designing Electronics with Linux
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Nice article, thanks for the
3 hours 43 min ago - I once had a better way I
9 hours 29 min ago - Not only you I too assumed
9 hours 46 min ago - another very interesting
11 hours 40 min ago - Reply to comment | Linux Journal
13 hours 33 min ago - Reply to comment | Linux Journal
20 hours 27 min ago - Reply to comment | Linux Journal
20 hours 43 min ago - Favorite (and easily brute-forced) pw's
22 hours 34 min ago - Have you tried Boxen? It's a
1 day 4 hours ago - seo services in india
1 day 8 hours ago
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Featured Jobs
| Linux Systems Administrator | Houston and Austin, Texas | Host Gator |
| Senior Perl Developer | Austin, Texas | Host Gator |
| Technical Support Rep | Houston and Austin, Texas | Host Gator |
| UX Designer | Austin, Texas | Host Gator |
| Web & UI Developer (JavaScript & j Query) | Austin, Texas | Host Gator |
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




Comments
wrong script
missing header.php you mention all the functinality of your header.php yet you havent shown the code :(