3

We are executing below curl call from PHP.

$url = $fullurl;

if (isset($url)) {
    $ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch , CURLOPT_RETURNTRANSFER, 1);
$data = curl_exec($ch);
$headers = curl_getinfo($ch);
curl_close($ch);

$check_url_status =  $headers['http_code'];
if ($check_url_status == '200')
    $ress = "Link Works";
else
    $ress =  "Broken Link";

}

What other HTTP status codes should we consider to check if the URL is not a broken /dead link.

Sybille Peters
  • 2,832
  • 1
  • 27
  • 49

1 Answers1

2

Remember the 5 HTTP Status code classes : 1xx Continue (protocol switching), 2xx OK, 3xx Redirect, 4xx client error, 5xx server error.

If your Curl client follow the redirections (3xx), I think you can just test that status code <= 299. All other status code will make a "broken link".

Depending on how deep is your test, you can also think of theses cases :

  • 401 Unauthorized/ 403 Forbidden : the ressource need authentification. It does not mean the link is broken, but that authorized client may see it, and other will not.
  • 204 No Content : the ressource is accessible but does not return any content. Some analytics ressources returns 204. But the visual result will be a broken image or a link to an empty page.

If your goal is to change the display of a broken link you can use Javascript to manage it client-side, but it can be limited to your domain. See this question

BenC
  • 1,647
  • 18
  • 25