?
Solved

Checking URLs

Posted on 2003-03-17
2
Medium Priority
?
246 Views
Last Modified: 2006-11-17
Can anybody give me tips on how to use PHP to check if a URL is valid or not or has changed (ie. that the website exists or has been updated)?
0
Comment
Question by:tl99
  • 2
2 Comments
 
LVL 15

Accepted Solution

by:
VGR earned 150 total points
ID: 8153824
already answered one day ago.

here's a complete "demo" script. Make good use of the CheckURI() function ;-)

<?
$links=array();
$links[1]='http://www.netscape.com';
$links[2]='http://www.badlink.zob';
$links[3]='http://www.experts-exchange.com';

//test
$DEBUGTEST=1;
if ($DEBUGTEST==1) echo count($links)." links in input<BR>";
//
// inits
$badlinks=0;
$bad=array();
// loop through $links[] (beforehand filled in by you)
for ($i=1;$i<=count($links);$i++) {
// try to access that link
$isgood=CheckURI($links[$i]);
if ($DEBUGTEST==1) echo "link $i '".$links[$i]."' is ".(($isgood)?'OK':'KO')."<BR>";
// memorize result
if (! $isgood) $bad[]=$i;
}
// display bad links
$badlinks=count($bad);
//test
if ($DEBUGTEST==1) echo "$badlinks bad links found<BR>";
//
for ($i=0;$i<$badlinks;$i++) echo "bad link '".$links[$bad[$i]]."' (index=$i)<BR>";
// done

function CheckURI($parurl) {
// inits
$result=TRUE;
// try to get URI
$filename = "$parurl";
$tobec=TRUE;
$fd = @fopen ($filename, "r");
if ($fd) { // si page trouvie
  while ((!feof ($fd))and($tobec)) {
    $ligne= fgets($fd, 4096);
    if (!(strpos($ligne,'[404] Not Found')===false)) $tobec=FALSE; // stop as soon as this is encountered
    $contents []=$ligne;
  } // while lecture bloquante
  fclose ($fd);
  if ($tobec) { // file entirely read OK (note that we could stop after X first lines, the '404' message is not at the 345th line...
    // nothing, result is TRUE already
    // this block is in case you want to log anything like "last correct date where found the URI was OK"
  } else { // we stopped before the end : 404 found
    $result=FALSE;
  }
} else { // page not found
  $result=FALSE;
} // if page trouvée ou non
return $result;
} // CheckURI Boolean Function
?>

0
 
LVL 15

Expert Comment

by:VGR
ID: 8217077
well, if my answer was "thorough", I hope to get a grade "A" as per EE guidelines :D
0

Featured Post

[Webinar] Database Backup and Recovery

Does your company store data on premises, off site, in the cloud, or a combination of these? If you answered “yes”, you need a data backup recovery plan that fits each and every platform. Watch now as as Percona teaches us how to build agile data backup recovery plan.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.
Suggested Courses

578 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question