Hi I am trying to figure out a code for checking redirects in a database, is there 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.
Asked
Active
Viewed 1,205 times
2
-
can you describe your data/database layout? It would be tough to know without more information. – Paul W May 25 '11 at 11:37
-
there is a column for id, redirect path, url, name of person requesting redirect, date, web person, active/inactive and modtime. – Bee May 25 '11 at 12:07
3 Answers
0
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;
}
}

inquam
- 12,664
- 15
- 61
- 101
-
The database has the path for the redirects and the original urls. I believe the redirects were created manually. There is information in the table as to whether or not the redirect is active or not. On the webpage it highlights the rows that are active or inactive but I need a way to check on the same page load whether the active links are still working. I looked at the cURL function as an option since posting the question but have never used it and unsure of how to implement it. – Bee May 25 '11 at 12:00
-
I couldn't get the curl to work so I used this: `$query_entries = "SELECT * from redirects order by code ASC"; $result_entries = mysql_query($query_entries); $result_num = mysql_num_rows($result_entries); while ($row = mysql_fetch_array($result_entries)) { if ($row['active'] == "y") { $url = $row['url']; $headers = get_headers($url, 1); if (!preg_match("/[2-3][0-9][0-9]/", $headers[0])) { //200-399 (table info...)}` It takes quite awhile to process the headers and I can't read https status codes. I was told I might be able to if I called a different port (443)... – Bee May 30 '11 at 19:08
0
Use this validator from w3schools.
Enter your site URL and make sure to check Check linked documents recursively, recursion depth
option

CristiC
- 22,068
- 12
- 57
- 89
-
I don't need to check the entire site, there are about 3000 pages. I just need to check the redirects in my database which number just over 400. – Bee May 25 '11 at 12:12