?
Solved

Checking URLs

Posted on 2003-03-17
2
Medium Priority
?
243 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Why Off-Site Backups Are The Only Way To Go

You are probably backing up your data—but how and where? Ransomware is on the rise and there are variants that specifically target backups. Read on to discover why off-site is the way to go.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Developers of all skill levels should learn to use current best practices when developing websites. However many developers, new and old, fall into the trap of using deprecated features because this is what so many tutorials and books tell them to u…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
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…
Suggested Courses

801 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