Version update checker script

Posted By: Ian on Jan 31, 2015 in PHP, Snippets

Question
How can i get my script to check my site for updates?

Answer
The script below checks the local version number against the remote one and if the local is lower than the remote it shows the update link else just shows the current version number.
<?php
/* local script version */
define('version', '2.1.0');
/**
* Version Checker
*
*/
function check_version()
{
$REMOTE_VERSION_URL = "http://ianjgough.com/wp-content/demos/bannerbar/version.txt";
/* if cURL is available, use it */
if (function_exists('curl_init')) {
$ch = curl_init($REMOTE_VERSION_URL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HEADER, 0);
curl_setopt($ch, CURLOPT_TIMEOUT, 10);
$REMOTE_VERSION = curl_exec($ch);
curl_close($ch);
} else {
/* if cURL is not available, use file_get_contents() */
$REMOTE_VERSION = file_get_contents($REMOTE_VERSION_URL);
}
/* if local version is less than remote version show update link */
if (version_compare(version, $REMOTE_VERSION, '<')) {
return "Version " . version . "&nbsp;&#124;&nbsp;<a href=\"http://ianjgough.com/wp-content/demos/bannerbar/loop.php\" title=\"Opens in New Window\" onclick=\"window.open(this.href);return false;\"><span class=\"external\">New Version Available</span></a>";
} else {
/* return local version number */
return "Version " . version;
}
}
print check_version();
?>

Download

Demo

AnonymousIan.J.Gough

Did this help you? Please Let me know by commenting below even if it’s just to say “Thanks”

leave a comment

About This Site

Dreams are built from lines of code well it's true!
If you can dream it you can usually code it.

I have learned so much from other people on the Internet and this site is for me to give back some of what i have learned and hope people can now learn from me.
Have fun and surf safely,
Ian.J.Gough

protected by copyscape duplicate content check

Categories


Powered by Banner Bar 125