tags:

views:

75

answers:

2

Hello folks

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?

+2  A: 

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)) {
}
Chris Kloberdanz
I prefer file_get_contents as well.
ChronoFish
But that's not why it's going slow.
Michael Johnson
yeah.. much simple..
bob
+3  A: 

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.

DOOManiac
I'd go for downloading the file only on occasion (every 'x' days, as DOOManiac says). This avoids relying on JavaScript, as, despite what you might think, many clients won't have JavaScript (mobile phones, text browsers (yes, they're still used), automated tools). And only do the download in a scheduled job so it doesn't impact the user.
Michael Johnson
accepted. I just make it call one time and store to db for the current version then compare it every x days. :)
bob