Hi I am trying to figure out a code for checking redirects in a database, is the开发者_JAVA百科re a way I can test the URLs for broken or dead links every time the script runs? I have been tinkering with get_header and http_send_status but I'm a newb and am probably not doing it right.
Using https://www.google.com/webmasters/tools/ is the key
Use this validator from w3schools.
Enter your site URL and make sure to check Check linked documents recursively, recursion depth
option
Do you mean redirect's done in PHP? Like with header()
or similar? Those links could be isolated and tested using cURL or similar to check if they exist. Do you need to check every link at each redirect or do you just want to check to be able to ignore these links in the future?
If so, the best idea would probably be to set up a cron job to do this every night and log all the broken links.
Edit:
Something like this prehaps?
function urlExists($url)
{
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
// Check return code to determine if page exists
if($httpcode >= 200 && $httpcode < 300){
return true;
} else {
return false;
}
}
精彩评论