473,399 Members | 4,192 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,399 software developers and data experts.

How to check an URL ..?

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
1 3267
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Craig Bailey | last post by:
Someone please explain what alternate universe I fell into this afternoon when PHP started telling me that 2 doesn't equal 2. Not sure about you, but when I run this, it tells me 59001.31 doesn't...
2
by: Askari | last post by:
Hi, How do for do a "select()" on a CheckButton in a menu (make with add_checkbutton(....) )? I can modify title, state, etc but not the "check state". :-( Askari
2
by: Edward | last post by:
The following html / javascript code produces a simple form with check boxes. There is also a checkbox that 'checks all' form checkboxes hotmail style: <html> <head> <title></title> </head>...
7
by: Tony Johnson | last post by:
Can you make a check box very big? It seems like when you drag it bigger the little check is still the same size. Thank you, *** Sent via Developersdex http://www.developersdex.com ***...
2
by: Travis.Box | last post by:
I have an MS Access userform with 16 Check Boxes. Each of the checkboxes has a different option value, which coincides with the Check Box name (eg. cb01.OptionValue = 1). At the bottom of the...
1
by: scprosportsman | last post by:
Please help guys, i am trying to set up a database here at work and im fairly new to access in terms of writing functions and queries and stuff. I have 2 different places on my design that will...
2
by: Chris Davoli | last post by:
How do you enable a check box in the GridView. I selected Checkbox Field in the Columns of the GridView, and the check box shows up in the Grid view, but it is disabled. How do I enable it so I can...
16
by: Brian Tkatch | last post by:
Is there a way to check the order in which SET INTEGRITY needs to be applied? This would be for a script with a dynamic list of TABLEs. B.
5
by: starke1120 | last post by:
Im creating a check in – check out database for RF guns. I have a table that contains models. ID (primary key) Model A table that contains Gun Details ID (primary key) Model_id...
1
by: ghjk | last post by:
my php page has 7 check boxes. I stored checked values to database and retrive as binary values. This is the result array Array ( => 0 => 1 => 0 => 1 => 0 => 0 => 1 ) 1 means checked....
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.