tags:

views:

68

answers:

5

I have a MySQL database with some URLs in it. One URL per row. Each URL has my script on it. What I am wanting to do, is check if the file is still there via a PHP script. Not check if it 404'd, but rather check if it has been modified or replaced. Is this possible? If so, how would it be accomplished?

I was thinking making the remote file echo some string, and having the local file check the page for that string, but that seems a little inefficient and sloppy.

EDIT: The remote files don't output any data to the remote users, they trigger internal scripts, so remote users would only see a white page. (The checking script would only see this as well)

A: 

if the urls are local files you can use filemtime on each file and check the time against the modifiedtime row in the table.

Galen
They aren't local. Sorry, you probably typed this up before I edited my question. I made a typo in the title. I accidentally put local instead of remote
Rob
+1  A: 

Bulletproof Way

  1. Fetch the URL contents.
  2. Compute the MD5() or SHA1() hash.
  3. Fetch the URL again and check if the hash has changed.

There are other ways (such as the Last-Modified HTTP header), but they don't work with all servers.

Alix Axel
Alright, sounds great. How could I compute the the MD5? Just get the contents using curl and then md5($contents)? Will this work even if there's no output from that remote file? (At least none visible to remote users)
Rob
@Rob: See my answer (http://stackoverflow.com/questions/2719012/automated-url-checking-from-a-mysql-table/2719162#2719162) on your other question, it addresses specifically that issue. =)
Alix Axel
http://us2.php.net/md5_file
Jamie
@Jamie: Only works with PHP 5.1+ but it's great, thanks.
Alix Axel
+1  A: 

Well you can ask to the file to send his md5 under certain conditions, for example:

if($_GET['md5hash']==true)
{ 
  echo md5($_SERVER['SCRIPT_FILENAME']);
}

You can easily understand if your script is changed by receiving his md5 or receiving something different by his md5, but i don't know what is the score for you.

Cesar
A: 

just use

md5_file('http://path.to/file');

and store the hash for checking later

Jamie
A: 

You may also be able to use stat on a remote file and check the mtime record.

Justin Johnson