开发者

PHP - How to check a script version

I have a small script and want to detect the script version from admin footer. On the admin footer.php I put this code.

<?php
  define('VERSION', '1.0');
  $fp = fopen("http://v.domain.com/version.txt", "r");
  while ($line = fgets($fp)) {
    开发者_StackOverflow  $line;
      if (VERSION != $line) {
?>
<div id="upgrade">
<a href="http://domain.com/download/" target="_blank">Download a new version [<?php echo $line; ?>]</a>
</div>
<?php
      }
  }
?>

In the version.txt only have 1.0.1 and working fine to compare the version.

Problem here, client site will getting slow. How to fix this problem?


The reason it is slow is because the server running your PHP code has to hit v.domain.com over HTTP and download a copy of version.txt. While this is happening, the PHP is sitting there waiting on it.

Why does this make your entire page slow even though it's in the footer? Because Apache will cache the output of the PHP page for a bit before spitting it out to the browser. And even if you flush the output buffer, sometimes the browser does and that's something you have no control over.

It sounds like you want to release some PHP-based tool, and have an auto-version-check in the footer, correct?

If so, there are a couple problems with doing it the way you're doing it:

  1. There's no need to check the version every single time your page loads. Save the last date you checked somewhere, and only check again ___ days later. (Hopefully you are already doing this and just cut it from the example for simplicity)

  2. Reading the file like this from another server is a bad idea. As you're experiencing now, it can cause a slowdown if your v.domain.com gets busy. If it goes down, then your PHP will take even longer because it's waiting on a timeout.

A better way to do this would be through Javascript. After your page is loaded, you would have a javascript function that uses AJAX. If you are unfamiliar with Javascript though there could be a bit of a learning curve, but this is the ideal way to handle your situation.


If the file is just the version number then I would suggest trying:

file_get_contents()

$version = file_get_contents('http://wwww.example.com');
if (VERSION != trim($version)) {
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜