7

How can I check the version of my script against an online file to see if it's the latest version?

For clarification, I'm talking about a script I wrote, not the version of PHP. I'd like to incorporate a way for the end user to tell when I've updated the script.

1
  • You should maybe rephrase the question to make it clear if you want the version of the script you are running or the PHP_VERSION Commented Oct 25, 2008 at 8:18

5 Answers 5

16

To specify the second (more simple) solution phjr proposed:

Have a file version.txt on your own public server and include the following function into your deployed project/script:

define('REMOTE_VERSION', 'http://your.public.server/version.txt');

// this is the version of the deployed script
define('VERSION', '1.0.1');

function isUpToDate()
{
    $remoteVersion=trim(file_get_contents(REMOTE_VERSION));
    return version_compare(VERSION, $remoteVersion, 'ge');
}

version.txt should just contain the most recent version number, e.g.:

1.0.2
Sign up to request clarification or add additional context in comments.

2 Comments

I am out of votes for the next 15 hours but want to upvote this answer.
Another solution that sounds great, I'll try this one as well.
2

Per comments on this answer

// Substitue path to script with the version information in place of __FILE__ if necessary
$script = file_get_contents(__FILE__);
$version = SOME_SENSIBLE_DEFAULT_IN_CASE_OF_FAILURE;
if(preg_match('/<!-- Script version (\d*(\.\d+)*) -->/', $script, $version_match)) {
    $version = $version_match[1];
}

1 Comment

I like that... I'll give that a try.
2
define('REMOTE_VERSION', 'http://your.public.server/version.txt');
define('VERSION', '1.0.1');
$script = file_get_contents(REMOTE_VERSION);
$version = VERSION;
if($version == $script) {
    echo "<div class=success> 
<p>You have the latest version!</p> 
</div>";
} else {
    echo "<div class=error> 
<p>There is a update available!</p> 
</div>";
}

Comments

0

Have an RSS or Atom feed with update info. Wordpress does something similar. Then you can locally save information which updates have been shown to the user etc.

For even simpler solution, have a file on project website that would contain just the version number. Then compare it with a version number stored in your program, probably within a constant.

Comments

-1

Change

if($version == $script) 

to

if($version >= $script)` 

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.