How to Check Broken Links with PHP

When a Web server presents a Web page to a user, it displays the page according to the HTML markup provided. If the HTML markup defines a link, the Web server will display the link. The Web server does not validate the link to make sure it is working. You can check for broken links on a Web page using PHP by searching for links using a regular expression and by checking each link using the PHP "cURL" functions.

Instructions

    • 1

      Create a new PHP file and include the HTML headers. Set a variable equal to the file name that will be checked for broken links and read the contents of the file into a variable. For example, type:

      <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

      <html xmlns="http://www.w3.org/1999/xhtml">

      <head>

      <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />

      <title>Link Checker</title>

      </head>

      <body>

      <?php

      $file = "somefile.html";

      $file = file_get_contents($file) || die("<p>Could not open file " . $file . "</p>");

      echo "<p>Checking links in " . $file . "</p>";

    • 2

      Initialize an array to store the links in the file. Use the "preg_match_all" function with the contents of the data file and a regular expression to locate links in the file. For example, type:

      $links = array();

      preg_match_all("/\<a.*?href=[\"|\'](.*?)[\"|\']\>/", $file, $links);

    • 3

      Iterate through each link in the file. Use cURL to open the link and then check the error code to determine if the link is good or bad. For example, type:

      foreach($links as $index=>$link) {

      $ch = curl_init($link);

      curl_setopt($ch, CURLOPT_URL, $link);

      curl_setopt($ch, CURLOPT_HEADER, 0);

      curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);

      curl_exec($ch);

      $result = "good";

      if (curl_errno($ch) != 0) $result = "bad";

      curl_close($ch);

      printf("<p>%s is a %s link<br/></p>", $link, $result);

      }

      ?>

      </body>

      </html>

Related Searches:

References

Resources

Comments

You May Also Like

Related Ads

Featured