Вопрос

Problem I have is some links in my pages are broken, and unless until someone reports it, there is no fix for it. can I automate so that every link in the page is "pinged" to check if it is alive or find alternative links? Is there an automation script in server side (php or so) to fix broken links in a page?

Это было полезно?

Решение

What I would do is..

Parse your HTML source of your webpage and then grab all the hyperlinks from the <a> anchor tags into an array and then do a file_get_contents() on each of them and detect the response headers and if they are NULL then the link is down, else the link is up.

Not tested though.. But syntactically correct

<?php

$html= file_get_contents('http://yourwebsite.com/index.html');
$dom = new DOMDocument;
$dom->loadHTML($html);

$links = array();
foreach ($dom->getElementsByTagName('a') as $tag) {
    $links[]=$tag->getAttribute('href');
}

foreach($links as $link)
{
 @file_get_contents($link);
 echo is_null((@$http_response_header)) ? "$link is Down" : "$link is Working";
}
Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top