PHP-如何检查脚本版本 [英] PHP - How to check a script version

查看:85
本文介绍了PHP-如何检查脚本版本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个小脚本,想从管理员页脚中检测脚本版本.在管理员footer.php上,我放了这段代码.

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
      }
  }
?>

在version.txt中只有1.0.1,并且可以很好地比较版本.

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?

推荐答案

之所以缓慢,是因为运行您的PHP代码的服务器必须通过HTTP访问v.domain.com并下载version.txt的副本.发生这种情况时,PHP就坐在那里等待.

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.

为什么即使页脚位于页脚中,这也会使整个页面变慢?因为Apache在将PHP页面的输出吐出到浏览器之前会对其缓存一会儿.即使刷新输出缓冲区,有时浏览器也会这样做,而这是您无法控制的.

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.

听起来您想发布一些基于PHP的工具,并在页脚中进行自动版本检查,对吗?

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. 页面每次加载时都无需检查版本.将您上次检查的日期保存在某个地方,仅在___天后再检查一次. (希望您已经在执行此操作,为简单起见,仅将其从示例中删除)

  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)

从另一台服务器读取这样的文件是一个坏主意.正如您现在所遇到的,如果您的v.domain.com忙起来,它可能会导致速度变慢.如果出现故障,则您的PHP将花费更长的时间,因为它正在等待超时.

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.

更好的方法是通过Javascript.页面加载后,您将拥有使用AJAX的javascript函数.如果您不熟悉Javascript,虽然可能会有一些学习上的弯路,但这是处理您的情况的理想方法.

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.

这篇关于PHP-如何检查脚本版本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆