How to regularly check for a newer version in a PHP application

Introduction

I was searching a quick way to inform people that a newer version of phpMemcachedAdmin was released, without needing to visit the website everyday.
I only found scripts that make HTTP request every time, so i wrote this little function to add some internal cache to the HTTP request.

Here is the result : a small script for a PHP application to check for a newer version, using a file hosted on code.google.com (File example here)

What it does

  • It stores a local file named .version where the latest version available is stored (Plain text like “1.2.0“)
  • Each time the local file is older than 15 day, it goes to code.google.com to check for the latest version (Again, the file only contains version number “1.2.0“)
  • It wrote the latest version available in the local file
  • If the file is younger than 15 day, it open the local file, and compare the version number stored in it with the application current version.

So with this small technique, only one HTTP request is done every 15 day, the rest of the time it’s only a local file, being the least intrusive in the user system.

PHP version check script

This script works in both Linux & Windows, use system temp path to wrote the latest version file, and also use the error suppression operator (@), to prevent warning on file_get_contents() function if the user does not have access to code.google.com.

Hope this little script will help you well.

Leave a Reply

Your email address will not be published. Required fields are marked *