05.05.2013 Views

Programming PHP

Programming PHP

Programming PHP

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Example 9-8. Caching dynamic buttons<br />

<br />

A Faster Cache<br />

Example 9-8 is still not quite as quick as it could be. There is a more advanced caching<br />

technique that completely eliminates <strong>PHP</strong> from the request once an image has<br />

been generated.<br />

First, create a buttons directory somewhere under your web server’s DocumentRoot<br />

and make sure that your web server user has permissions to write to this directory.<br />

For example, if the DocumentRoot directory is /var/www/html, create /var/www/html/<br />

buttons.<br />

Second, edit your Apache httpd.conf file and add the following block:<br />

<br />

ErrorDocument 404 /button.php<br />

<br />

This tells Apache that requests for nonexistent files in the buttons directory should be<br />

sent to your button.php script.<br />

Third, save Example 9-9 as button.php. This script creates newbuttons, saving them<br />

to the cache and sending them to the browser. There are several differences from<br />

Example 9-8, though. We don’t have form parameters in $_GET, because Apache handles<br />

error pages as redirections. Instead, we have to pull apart values in $_SERVER to<br />

find out which button we’re generating. While we’re at it, we delete the '..' in the<br />

filename to fix the security hole from Example 9-8.<br />

Dynamically Generated Buttons | 225<br />

This is the Title of the Book, eMatter Edition<br />

Copyright © 2002 O’Reilly & Associates, Inc. All rights reserved.

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!