Eli : Memcached, MySQL, Highcharts

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.

# Actual application version
define('CURRENT_VERSION', '1.2.1');
 
/**
 * Check for the latest version, from local cache or via http
 * Return true if a newer version is available, false otherwise
 *
 * @return boolean
 */
function check()
{
    # Configuration array
    $ini = array('local_path' => sys_get_temp_dir() . DIRECTORY_SEPARATOR . '.version',
                 'distant_path' => 'http://phpmemcacheadmin.googlecode.com/files/latest',
                 'time_between_check' => 15*24*60*60);
 
    # Checking if file was modified for less than $ini['time_between_check'] ago
    if((is_array($stats = @stat($ini['local_path']))) && (isset($stats['mtime'])) 
       && ($stats['mtime'] > (time() - $ini['time_between_check'])))
    {
        # Opening file and checking for latest version
        return (version_compare(CURRENT_VERSION, file_get_contents($ini['local_path'])) == -1);
    }
    else
    {
        # Getting last version from Google Code
        if($latest = @file_get_contents($ini['distant_path']))
        {
            # Saving latest version in file
            file_put_contents($ini['local_path'], $latest);
 
            # Checking for latest version
            return (version_compare(CURRENT_VERSION, $latest) == -1);
        }
        # Can't connect to Google Code
        else
        {
            # In case user does not have access to code.google.com !!!
            # Here it's up to you, you can write nothing in the file to display an alert
            # leave it to check google every time this function is called
            # or write again the file to advance it's modification date for the next HTTP call.
        }
    }
}

Hope this little script will help you well.

Related Posts





comments powered by Disqus

You may also be interested in