Jump to content

Creating an upgrade system


Recommended Posts

So I have a program that I am going to give out on my site, but I know like a handfull of users are going to actually go back to my site to check for an upgrade, so I want to create a notification in the backend to say if its up to date or if a newer version is available.what would be the best method to accomplish this?

Link to comment
Share on other sites

Have a constant in your program that defines the version. Or maybe a few constants.

//version 1.4.2define("MAJOR_VERSION", 1);define("MINOR_VERSION", 4);define("REVISION", 2);

And have a text file on your server that contains similar statements.

<?phpdefine("CURRENT_MAJOR_VERSION", 1);define("CURRENT_MINOR_VERSION", 5);define("CURRENT_REVISION", 4);?>

But name this file .txt, not .php. When you pull it up in a web browser you want to see the PHP code. Then, you need a little chunk in your program to 1) check for a connection to your server, 2) include the file, and 3) compare the versions. To check for a connection, you can open a socket on port 80 and see if it connects. To include the file, you will probably want to use fopen for compatibility.

$file_source = "http://server.com/file.txt";  $file_source = str_replace(' ', '%20', html_entity_decode($file_source)); // fix url format  if (($rh = fopen($file_source, 'rb')) === FALSE) { die ("update file not readable"); }  $code = "";  while (!feof($rh))  {	$code .= fread($rh, 1024);  }  fclose($rh);  if ($code != "")	eval ($code);

That will grab the contents of the file and execute the PHP code. Since you are executing arbitrary code, it's very important that the source file (the .txt file) is NOT writable. A hacker with a mind to break everyone would be able to overwrite that file with any of their own code and it would get executed. You can do a check, since you know how big the text file should be within a few bytes, but it's not foolproof.So once you grab the code and execute it, then you just need to compare the constants.

<?php$update = false;if (CURRENT_MAJOR_VERSION > MAJOR_VERSION)  $update = true;elseif (CURRENT_MAJOR_VERSION == MAJOR_VERSION){  if (CURRENT_MINOR_VERSION > MINOR_VERSION)	$update = true;  elseif (CURRENT_MINOR_VERSION == MINOR_VERSION)  {	if (CURRENT_REVISION > REVISION)	  $update = true;  }}if ($update){  //an update is available}?>

Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

  • Create New...