Loading Ad Javascript with PHP

I serve ads on the Drudge Retort using Blogads, a great ad broker that occasionally has trouble serving the ads. When this happens, pages on the Retort load more slowly because they can't fetch a Javascript program and CSS stylesheet required by Blogads.

I decided to fix this problem by writing Cache Remote File, a PHP script that performs three functions:

  • Save a cached copy of a remote file
  • Display the cached copy for 10 minutes before requesting the file again
  • Display the cached copy when the remote server is offline or slow

The script will give up trying to load the remote file after three seconds, which keeps it from hanging when the remote server is having difficulties. It can be customized to load any URL of any content type and requires PHP 4 or higher with cURL support. I've released it under the GPL. Let me know if you have any problems with it or can improve the script.


You suck!

Reporting on Cache Remote File, a PHP script is well within your rights.

It is also sleazeball shit.

It took me a couple of hours to get the meaning of your comment. I'm slipping.

Nice algorithm, but I would have made it a function to which you could pass the URL and local filename, so you could easily cache more than one such page. I also wouldn't echo the result, I'd just return it and let the caller echo it if desired.


echo get_cached_remote_page($url, $localfile);


Add a Comment

All comments are moderated before publication. These HTML tags are permitted: <p>, <b>, <i>, <a>, and <blockquote>. This site is protected by reCAPTCHA (for which the Google Privacy Policy and Terms of Service apply).