00:00
00:00
Newgrounds Background Image Theme

Chan99 just joined the crew!

We need you on the team, too.

Support Newgrounds and get tons of perks for just $2.99!

Create a Free Account and then..

Become a Supporter!

Php: Link Checking

3,299 Views | 8 Replies
New Topic Respond to this Topic

Php: Link Checking 2005-12-15 22:37:36


PHP : Main

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!

Response to Php: Link Checking 2005-12-15 22:53:40


Thats pretty good =).


BBS Signature

Response to Php: Link Checking 2005-12-16 02:19:10


It doesn't work on sane hosts, they disables opening urls as normal files.
The solution is to use the cUrl libary instead. It's even bundled with php.


Each time someone abuses hittest, God kills a kitten. Please, learn real collision testing.

Response to Php: Link Checking 2005-12-16 07:39:39


Hey, here's an idea, that you just gave me, you could check if it works on your forum, and if it doesn't, it could say like broken, or be in a different color, like black or something. I dunno just an idea, if I get the time, I'll change your script a little, or maybe you could, cuz I'm gonna be busy. =-P

Response to Php: Link Checking 2005-12-16 11:52:39


At 12/16/05 07:39 AM, Drake_SI wrote: Hey, here's an idea, that you just gave me, you could check if it works on your forum, and if it doesn't, it could say like broken, or be in a different color, like black or something. I dunno just an idea, if I get the time, I'll change your script a little, or maybe you could, cuz I'm gonna be busy. =-P

The only time you would check that is when the url is submitted in text, because it would be very uneffecent to do it for every post on a forum. So that would not be a good idea. If a url was broken when it was submitted, it does not mean it will be broken later when a page containing the link is viewed, just as it may be active when submitted, but not later. So I would not recomend color coding the links or anything

Response to Php: Link Checking 2005-12-16 12:42:47


I would simply not recommand using this function, ever.

Sure it might be fun to know if a site is down or not, but you really don't want to put that much strain on your server. Maybe you could use it if someone submits a link to an affiliate program, but that's really the only way I could see it used sanely.

Good idea anyways. You probably shouldn't use fopen for urls, though. But I'm guessing you wouldn't know how to open a raw socket to a site and send an HTTP header to it, and then analyze its response.


BBS Signature

Response to Php: Link Checking 2005-12-16 13:38:41


so would it be more usable on your own pages and not to check other site pages?


[PHP: Main]-[AVGN Club]

"You know a dame is classy when she got some jiggle in her assy." - Unknown

BBS Signature

Response to Php: Link Checking 2005-12-16 22:05:22


At 12/16/05 01:38 PM, JimmyDallas wrote: so would it be more usable on your own pages and not to check other site pages?

yup, but you know I was just having fun, I know it would be unefficient, but if you are using this function, then you probably don't care.

Response to Php: Link Checking 2005-12-18 18:26:11


if it's inefficient, just check the sites every hour to see if they are up (Using a cron built into the php script).