thanks, i hadn't realised you could use fsock for an actuall link.
I have just tried something out of a book i have to see if it would work, but im having problems with HTTP response codes.
Basically if a link doesn't exist i get 404 which is great but when it does exist i get 200 and 400.
Now thats not right as 400 means Bad request. Anyone got any idea why this might be?
I will post the link to it running and the code below.
http://62.31.88.74/link.php
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
"http://www.w3.org/TR/2000/REC-xhtml1-20000126/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Validate URLs</title>
</head>
<body>
<?php
$db_connection = mysql_connect ("xxxxxxx", "xxxxx", "xxxxx") OR die (mysql_error());
$db_select = mysql_select_db ("tester") or die (mysql_error());
// This function will try to connect to a URL.
function check_url ($url) {
// Break the URL down into its parts.
$url_pieces = parse_url ($url);
$path = ($url_pieces['path']) ? $url_pieces['path'] : "/";
$port = ($url_pieces['port']) ? $url_pieces['port'] : 80;
// Connect.
if ($fp = fsockopen ($url_pieces['host'], $port, &$errno, &$errstr, 5)) {
fputs ($fp, "HEAD $path HTTP/1.1\r\n\r\n");
fputs($fp, "HOST: $url_pieces[host]\r\n");
fputs($fp, "CONNECTION: close\r\n\r\n");
$data = fgets ($fp,128);
fclose($fp);
$array = explode (" ", $data);
return $array[1]; // Return the HTTP code.
} else {
return $errno; // Return the error message.
}
}
// Query the database.
$query = "select * from php_links order by id";
$query_result = mysql_query ($query);
// Create a table.
echo '<table border="1" cellspacing="2" cellpadding="2" align="center">';
echo '<tr><td align="left"><b>Site Name</b></td><td align="left"><b>URL</b></td><td align="left"><b>HTTP Code</b></td></tr>';
// Check each URL.
while ($row = mysql_fetch_object ($query_result)) {
$check = check_url ($row->url);
echo '<tr><td align="left">' . $row->name . '</td><td align="left"><a href="'. $row->url . '" target="_new">' . $row->url . '</a></td><td align="left">' . $check . '</td></tr>';
}
?>
</table>
</body>
</html>
Cheers very much, Craig