PHP Check For Updates Script

This is the beginning of a new series: Code Snippets, where I do a small coding tutorial whenever I have time. Note that I am not an expert in various coding languages, so please do not flame me if there are better ways to code – I am just sharing what I know.

Anyway, today we are going to do an updating checking script. You have seen many desktop applications that automatically check for applications, but slowly, many web scripts available for download are also implementing this feature (eg. WordPress). And so in this tutorial, I would teach you how to go about creating a PHP script that checks your server for updates. Read on for the full tutorial.

There are generally 2 ways to go about checking for updates – checking against a database or checking against a file. Both have their advantages and disadvantages, so I will do a short break down here first:

Checking against a database
For those scripts that use a database, it is generally easier for the server admin (meaning you) to interface with the server frontend through a PHP script. Also, it is also more likely to work across different server configurations and do not require allow_url_fopen to be enabled in the PHP configuration. However, the main issue is security, as you would generally have to leave the access credentials on the file you distribute (of course, this would not be a cause for worry if you encode your scripts – I use phpCipher). But of course encoding scripts brings with it another set of problems such as requiring Zend Optimizer to be installed, but that is outside the scope of discussion. Also, you would need to enable remote database access.

Checking against a file
This is actually the method that would be covered in the tutorial. It is generally easier to secure a script that checks against a file as the update file is stored on your server after all. However, this method requires allow_url_fopen to be enabled in your PHP configuration, which may not always be a given (though it is ON with the default PHP configuration).

So now that you are clear about the various advantages and disadvantages, let’s get on with the actual tutorial. For this tutorial, I would only be covering the “checking against a file” method as it is easier to deploy.

Update File
First, we need to create a file that will reside on your update server. I would create a CSV (Comma Separated Values) file as PHP does provide an excellent fgetcsv() function for me to work with. You can create CSV files in any plain ol’ text editor.

fgetcsv() will return the data in an array, so for example, if we have a CSV file like:


And we parse the file using the fgetcsv() function,

$data = fgetcsv("helloworld.csv");

Then, we can access the data as:

print $data[0]; //prints "hello"
print $data[1]; //prints world;

With that in mind, for the purpose of the tutorial, we would create a CSV file with the following fields:


To make life easier for myself, I generally give the script build the value of the date of release. For example, if the script was released on the 1st of August 2009, it would be given a build value of 20090801 (yyyymmdd). This way, we can use simple mathematical conditions (greater than and lesser than) to determine if the script is outdated.

Of course, more fields can also be added if you require them.

Updating Checking Script
This would be the actual file that you include in your script distributions. First, we begin with a session_start() function. This would be further explained later. Then, we define the current script version:

< ?php
$version = "20090801";

Next, we define 2 variables: $critical and $update. The $critical variable will be used whenever there is a critical update (based on the priority field in the CSV file) and can act as a killswitch to maintain the integrity of the script (though of course, you can choose how to handle it). We set both variables to FALSE first.

$critical = FALSE;
$update = FALSE;

Following which, we check if the update server has been pinged already in the current browser session. This is both to speed up the execution of script as well as to prevent excessive load on your update server. So, we will use the condition

if (!$_SESSION['updateping']) { ... }

Since the $_SESSION['updateping'] variable is not yet set, that means that the update server has not been pinged in the current session and so we will proceed with the update checking. We would also tell the script to stop pinging the server in this session. Hence, inside the conditional,

$url = "";
$fp = @fopen ($url, 'r') or print ('UPDATE SERVER OFFLINE');
$read = fgetcsv ($fp);
fclose ($fp); //always a good idea to close the file connection
$_SESSION['updateping'] = TRUE;

Note that in the 8th line, we use the @ sign. This is to suppress any PHP error caused by the function not working (probably due to the wrong allow_url_fopen setting).

And here’s where the actual fun begins. First, we check if there is a critical update (which is denoted as a value 1 under the priority field. Next, we then check if the script build listed on the update server is greater than the script version listed in the file.

if ($read[0] > $version && $read[2] = "1") { $critical = TRUE; }
if ($read[0] > $version) { $update = TRUE; }

Next, we then display a suitable message for each case. Of course, this is be integrated into the previous conditional, but I am allowing for greater flexibility here. So, when there is a critical update available:

if ($critical) { 
print '

There is a critical update available!

'; print '


'; print '

You can get it at '.$read[3].'

'; die(); //terminate the script }

Note that $read[1] is actually the builddescription field in the CSV file. You can also experiment with HTML tags and other formatting options etc. $read[3] corresponds to the downloadurl field in the CSV file. Also take note that when using the print() function with a single inverted comma, variables will not be parsed and hence we have to break it up with periods (.).

Here, we handle the condition where there is a non-critical update:

else if ($update)
print '

There is an update available

'; print '


'; print '

You can get it at '.$read[3].'

'; }

Without the die() function, the script can continue running.

We then end off the original conditional in line 6 and the script with

else {
print '';

The above file can then be included in any of your script with a simple

include ("update.php"); //alternatively, request() can be used if you want to cause a fatal error if the file is not found

Please leave a comment if there is a bug in the above code or if there are better ways of writing the code. Thanks.


  1. Hi,

    thx for this script… but in Line 12 it should be
    if ($read[0] > $version && $read[2] == “1”) { $critical = TRUE; }


  2. Yup, that’s another way of doing it, but that would require the field to take on the value of “1”

    However, my original intention is to set the $critical variable to TRUE as long as there is a value for the priority field.

  3. don ever get into php unless u want to sacrifise all ur dream about mnc’s and handsome package…u will never get these in PHP developmenmt…

Leave a comment

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