note 45224 deleted from ref.url by aidan
| From: | aidan@php.net | Date: | Tue, 31 Aug 2004 06:56:58 +0000 |
| Subject: | note 45224 deleted from ref.url by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-75719@lists.php.net to get a copy of this message | ||
Note Submitter: Citrus.be
----
For people that want a nice script for validating their urls. I have found this method to be pretty
accurate, currently using this method to validate over 10.000 links every day. It gives no stress or
overhead to your server since all it does is open a socket... it will NOT retrieve the entire page.
// Syntax: list($result, $http_response) = validate_url('url');
//
// $result = 0 (url does NOT work), 1 (url works) or 2 (not sure)
// $http_response = W3 standard http respons (i.e. HTTP/1.1 301 Moved Permanently)
//
function validate_url($link) {
$url_parts = @parse_url( $link );
if(empty( $url_parts["host"])) {
return array(0, "NO HOST");
}
if(!empty( $url_parts["path"])) {
$documentpath = $url_parts["path"];
} else {
$documentpath = "/";
}
if(!empty( $url_parts["query"])) $documentpath .= "?" .
$url_parts["query"];
$host = $url_parts["host"];
$port = $url_parts["port"];
// Now (HTTP-)GET $documentpath at $host";
if(empty($port)) $port = "80";
$socket = @fsockopen( $host, $port, $errno, $errstr, 15 );
if(!$socket) {
$http_response = "";
return array(0, "Could not resolve");
} else {
fwrite ($socket, "GET ".$documentpath." HTTP/1.0\r\nHost: $host\r\n\r\n");
unset($http_response);
$http_response = trim(fgets( $socket ));
$result = 2;
if(preg_match("# [23][0-9][0-9] #", $http_response)) $result = 1;
elseif(preg_match("# [45][0-9][0-9] #", $http_response)) $result = 0;
fclose( $socket );
return array($result, $http_response);
}
}
list($result, $http_response) = validate_url("http://www.start.be");
echo "<p>Server replied '".$http_response."' wich is a ";
switch($result) {
case 0: echo "<font color=red>negative</font>"; break;
case 1: echo "<font color=green>positive</font>"; break;
case 2: echo "<font color=orange>weird</font>"; break;
}
echo "result</p>";