Diese funktion prüft den Staus einer Webseite, bzw. ob diese überhaupt existiert und liefert den HTTP-Fehlercode (oder DNS Error) zurück.
Gut geeignet um in seinen Scripts (z.B. bei Gästebüchern) die Links zu überprüfen.
Zum testen ruft man die Funktion wie folgt auf:
<?
$test = url_validate("<a href="http://www.tech-island.com/");" target="blank">http://www.tech-island.com/");</a>
echo "test=[$test]";
?> |
<?
$test = url_validate("<a href="http://www.tech-island.com/");" target="blank">http://www.tech-island.com/");</a>
echo "test=[$test]";
?>
/*
<ul>
<li> @return status code or HTTP request</li>
<li> @param string $link</li>
<li> @desc Überprüft die angegeben URL auf Erreichbarkeit und</li>
<li> liefert den HTTP Code oder DNS-Error zurück</li>
<li>/</li>
</ul>
function url_validate($link)
{
// Parse URL and set default values if neccesary
$url_parts = parse_url( $link );
if (empty($url_parts["host"]))
return(false);
if (!empty($url_parts["path"]))
$documentpath = $url_parts["path"];
else
$documentpath = "/";
if (!empty( $url_parts["query"]))
$documentpath .= "?" . $url_parts["query"];
// Get Host and Port from URL, Port 80 default
$host = $url_parts["host"];
$port = $url_parts["port"];
if (empty($port))
$port = "80";
// Now (HTTP-)GET $documentpath at $host";
$socket = fsockopen( $host, $port, $errno, $errstr, 30 );
if (!$socket)
{
// If socket can't bew connected there's a DNS-Error
$status = "dns-error";
}
else
{
// Get Status Code
fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0rnHost: $hostrnrn");
$http_response = fgets($socket, 30);
if (ereg("200 OK", $http_response, $regs ))
$status = "200";
elseif (ereg("404", $http_response, $regs ))
$status = "404";
elseif (ereg("30", $http_response, $regs))
$status = "30*";
else
$status = $http_response;
fclose($socket);
}
return ($status);
} |
/*
<ul>
<li> @return status code or HTTP request</li>
<li> @param string $link</li>
<li> @desc Überprüft die angegeben URL auf Erreichbarkeit und</li>
<li> liefert den HTTP Code oder DNS-Error zurück</li>
<li>/</li>
</ul>
function url_validate($link)
{
// Parse URL and set default values if neccesary
$url_parts = parse_url( $link );
if (empty($url_parts["host"]))
return(false);
if (!empty($url_parts["path"]))
$documentpath = $url_parts["path"];
else
$documentpath = "/";
if (!empty( $url_parts["query"]))
$documentpath .= "?" . $url_parts["query"];
// Get Host and Port from URL, Port 80 default
$host = $url_parts["host"];
$port = $url_parts["port"];
if (empty($port))
$port = "80";
// Now (HTTP-)GET $documentpath at $host";
$socket = fsockopen( $host, $port, $errno, $errstr, 30 );
if (!$socket)
{
// If socket can't bew connected there's a DNS-Error
$status = "dns-error";
}
else
{
// Get Status Code
fwrite ($socket, "HEAD ".$documentpath." HTTP/1.0rnHost: $hostrnrn");
$http_response = fgets($socket, 30);
if (ereg("200 OK", $http_response, $regs ))
$status = "200";
elseif (ereg("404", $http_response, $regs ))
$status = "404";
elseif (ereg("30", $http_response, $regs))
$status = "30*";
else
$status = $http_response;
fclose($socket);
}
return ($status);
}