Thread: Version Checker
View Single Post
Old 03-02-2008, 11:35 PM   #7 (permalink)
DeMo
The Contributor
 
DeMo's Avatar
 
Join Date: Jan 2008
Location: Brazil
Posts: 77
Thanks: 14
DeMo is on a distinguished road
Default

Here's a quick example, first we setup the XML file to hold the version information:
Code:
<?xml version="1.0" encoding="utf-8"?>
<version>
  <number>2.3.0</number>
  <releasedate>2008-03-02</releasedate>
  <download>http://www.mysite.com/mysoft_230.zip</download>
</version>
Now for the PHP part.
We'll use a variable $current_version to hold the current version number. This can come from a database, from a file on the disk, another variable, a constant, you name it. Let's assume it came from somewhere and it's value is 2.2.0, so our software is not updated.

PHP Code:
<?php
  
// The current version
  
$current_version "2.2.0";

  
// Let's load the XML with the version information
  
$xml simplexml_load_file('http://www.mysite.com/version.xml');

  
// Do the version check
  
if ($current_version != $xml->number) {
    
// Versions are different
    
echo 'Your software is out of date!<br />';
    echo 
'Latest version: ' $xml->number '<br />';
    echo 
'Released: ' $xml->releasedate '<br />';
    echo 
'Download: ' $xml->download;
  } else {
    
// Versions are equal
    
echo 'Your software is up to date!';
  }
?>
As you see, it compares $current_version to $xml->number and if they're different it shows the version information, if they're equal it shows a message that the software is updated.

This is a very simple system, if you change the version number to a lower version (say 1.1.0) it will still tell you that you need to update. One way to solve this is to remove the dots from the version numbers and then compare them, this way 2.2.0 becomes 220 and 1.1.0 becames 110, since 220 > 110 you know that you're up to date.
Send a message via ICQ to DeMo Send a message via MSN to DeMo Send a message via Skype™ to DeMo
DeMo is offline  
Reply With Quote