PHP - How to check a script version

▼魔方 西西 提交于 2019-12-11 19:51:38

问题


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)) {
      $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?


回答1:


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.




回答2:


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)) {
}


来源:https://stackoverflow.com/questions/1652964/php-how-to-check-a-script-version

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!