By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
455,051 Members | 1,258 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 455,051 IT Pros & Developers. It's quick & easy.

How to check an URL ..?

P: n/a
Jay
Hi !

I am a beginner in PHP. Does anybody know how to check if an URL is
alive or dead in PHP (not exist or server down...or whatever ) ?

Any help would be appreciated !

Thanks !

Jay
Jul 17 '05 #1
Share this Question
Share on Google+
1 Reply


P: n/a
I know two codes:
-first is simple
<?
function checkurl($url)
{
$phpnet = fsockopen($url, 80, &$errno, &$errstr, 30);
if(!$phpnet) {
echo "<b>php.net <font color=\"red\">Dead link!</font></b>\n"; }
else {
echo("<a href=\"http://".$url."\">".$url."</a>");
}
}
?>

-second more complex
<?
$text['N/A'] = "URL is not HTTP";
$text[OK] = "OK";
$text[FAIL] = "FAIL";
$text[DNS] = "Server not resolve";
$text[100] = "Continue";
$text[101] = "Switching Protocols";
$text[200] = "OK";
$text[201] = "Created";
$text[202] = "Accepted";
$text[203] = "Non-Authoritative Information";
$text[204] = "No Content";
$text[205] = "Reset Content";
$text[206] = "Partial Content";
$text[300] = "Multiple Choices";
$text[301] = "Moved Permanently";
$text[302] = "Found";
$text[303] = "See Other";
$text[304] = "Not Modified";
$text[305] = "Use Proxy";
$text[307] = "Temporary Redirect";
$text[400] = "Bad Request";
$text[401] = "Unauthorized";
$text[402] = "Payment Required";
$text[403] = "Forbidden";
$text[404] = "Not Found";
$text[405] = "Method Not Allowed";
$text[406] = "Not Acceptable";
$text[407] = "Proxy Authentication Required";
$text[408] = "Request Timeout";
$text[409] = "Conflict";
$text[410] = "Gone";
$text[411] = "Length Required";
$text[412] = "Precondition Failed";
$text[413] = "Request Entity Too Large";
$text[414] = "Request-URI Too Long";
$text[415] = "Unsupported Media Type";
$text[416] = "Requested Range Not Satisfiable";
$text[417] = "Expectation Failed";
$text[500] = "Internal Server Error";
$text[501] = "Not Implemented";
$text[502] = "Bad Gateway";
$text[503] = "Service Unavailable";
$text[504] = "Gateway Timeout";
$text[505] = "HTTP Version Not Supported";

function check($url) {
if (eregi( "^<a href="ftp://" target="_blank">ftp://</a>", $url)) {
$urlArray = parse_url($url);
$host = $urlArray[host]; $user = $urlArray[user]; $pass =
$urlArray[pass];
if ($user == "") { $user="anonymous"; $pass="de***@ps.xaos.ru"; }
$ftp = ftp_connect($host);
if ($ftp && ftp_login($ftp, $user, $pass)) {
$list = ftp_nlist($ftp, $urlArray[path]);
if ($list[0]!="") {
$return[code] = "OK"; return $return;
} else { $return[code] = "FAIL"; return $return;}
} else { $return[code] = "DNS"; return $return;}
}
if (eregi( "^<a href="http://" target="_blank">http://</a>", $url)) {
$urlArray = parse_url($url);
if (!$urlArray[port]) $urlArray[port] = "80";
if (!$urlArray[path]) $urlArray[path] = "/";
$sock = fsockopen($urlArray[host], $urlArray[port], &$errnum,
&$errstr);
if (!$sock) $return[code] = "DNS";
else {
$dump .= "GET $urlArray[path] HTTP/1.1
";
$dump .= "Host: $urlArray[host]
Connection: close
";
$dump .= "Connection: close

";
fputs($sock, $dump);
while ($str = fgets($sock, 1024)) {
if (eregi("^http/[0-9]+.[0-9]+ ([0-9]{3}) [a-z ]*", $str))
$return[code] = trim(eregi_replace("^http/[0-9]+.[0-9]+
([0-9]{3}) [a-z ]*", "\1", $str));
if (eregi("^Content-Type: ", $str))
$return[contentType] = trim(eregi_replace("^Content-Type: ",
"", $str));
}
fclose($sock);
flush();
}
} else $return[code] = "N/A";
return $return;
}
$res = check (stripslashes($url));
echo "Checking - ".stripslashes($url)." :: Status - ".$text[$res[code]];
?>

"Jay" <ar********@yahoo.com> ???????/???????? ? ???????? ?????????:
news:56**************************@posting.google.c om...
Hi !

I am a beginner in PHP. Does anybody know how to check if an URL is
alive or dead in PHP (not exist or server down...or whatever ) ?

Any help would be appreciated !

Thanks !

Jay

Jul 17 '05 #2

This discussion thread is closed

Replies have been disabled for this discussion.