Link Checking in PHP
Nothing is worse than a broken link. In this tutorial you will learn how to create a script in PHP that checks to see if a link is working or not. The script may be simple but I will go through each step as best as possible.
Lets start by adding the PHP opening tag
<?php
Next let's define the link we would like to check for functionality
$link = "http://newgrounds.com/bbs";
Next lets display what are script is doing while we wait for the results
echo "Checking: $link<br><br>\n"; flush();
What does the fluch function do? Taken directly from PHP.net-
Flushes the output buffers of PHP and whatever backend PHP is using (CGI, a web server, etc). This effectively tries to push all the output so far to the user's browser.
Next lets define our checking
$fp = @fopen($link, "r");
fopen() binds a named resource, specified by filename, to a stream.
Now lets display results if they come out negative
if (!$fp) { echo "The link is dead!"; }
and if it works...
else { fclose($fp); echo "The link is working!"; }
and finally lets close it off
?>
Hopefully this script taught you just more than how to check for links, and taught you about new usable functions that can be usable in lots of different ways. This tutorial was made by Jams44 for PHP: Main. The full script can be copied from here-
<?php
$link = "http://sucks.com/my.zip";
echo "Checking: $link<br><br>\n"; flush();
$fp = @fopen($link, "r");
if (!$fp) { echo "The link is dead!"; }
else { fclose($fp); echo "The link is working!"; }
?>
Peace and enjoy!