2008-10-25 63 views
7

如何檢查我的腳本版本與在線文件是否是最新版本?PHP腳本版本檢查/通知

爲了澄清,我正在談論我寫的腳本,而不是PHP的版本。我想爲最終用戶提供一種方式來告訴我何時更新了腳本。

+0

你也許應該改寫這個問題要清楚,如果你想你所運行的腳本或PHP_VERSION – Ken 2008-10-25 08:18:54

回答

15

要指定phjr提出的第二(更簡單的)解決方案:

有自己的公共服務器上的文件version.txt幷包括以下功能到您的部署項目/腳本:

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應該只包含最新版本麻木呃,如:

1.0.2 
+0

我在未來15個小時內的選票中缺貨,但想要upvote這個答案。 – eyelidlessness 2008-10-25 08:42:52

0

有一個RSS或Atom 飼料與更新信息。 WordPress的做了類似的事情。然後,您可以在本地保存更新已顯示給用戶的信息等。

對於更簡單的解決方案,請在項目網站上包含僅包含版本號的文件。然後將它與存儲在程序中的版本號進行比較,可能在一個常量內。

2

每評論上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]; 
} 
+0

我喜歡的版本......我給一個嘗試。 – PHLAK 2008-10-25 08:41:54

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>"; 
}