473,969 Members | 27,292 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3292
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\">D ead link!</font></b>\n"; }
else {
echo("<a href=\"http://".$url."\">".$u rl."</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] = "Unauthoriz ed";
$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] = "Preconditi on Failed";
$text[413] = "Request Entity Too Large";
$text[414] = "Request-URI Too Long";
$text[415] = "Unsupporte d Media Type";
$text[416] = "Requested Range Not Satisfiable";
$text[417] = "Expectatio n 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="anonymou s"; $pass="de***@ps .xaos.ru"; }
$ftp = ftp_connect($ho st);
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($urlA rray[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_repl ace("^http/[0-9]+.[0-9]+
([0-9]{3}) [a-z ]*", "\1", $str));
if (eregi("^Conten t-Type: ", $str))
$return[contentType] = trim(eregi_repl ace("^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********@yah oo.com> ???????/???????? ? ???????? ?????????:
news:56******** *************** ***@posting.goo gle.com...
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
4225
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 equal 59001.31. Change each side of the equation by a hundreth or two and it checks. Change it a bit more, and it won't. _What_ is going on here?! <?php
2
3242
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
2449
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> <body> <form name="myform" action=test.php method=post>
7
29989
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 *** Don't just participate in USENET...get rewarded for it!
2
27845
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 form is a command button. When the command button is clicked, I need the VBA scripting to determine which Check Box has been selected, and then assign a vaule to a string based on which Check Box has been selected. What I'm having trouble figuring...
1
4284
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 require checking a check box to tell which category something will pertain to. Well after each record is entered i want the check to remain with that record and auto clear when going to the next record so i can input something else if i have a...
2
3609
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 check/uncheck it. Also, what event do I use to check it for a value? Any articles out there? -- Chris Davoli
16
5693
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
11673
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 Gun_Number (assigned gun number by operations)
1
2432
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. I want to display these results in check boxes. This is my code. But it doesn't work <tr><td><input value="1" type="checkbox" id="1" name="check" <?php if ($array==1) echo "value=checked";?>/>Fasting 6 hours</td> <td><input value="2"...
0
10338
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
10149
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
11802
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
11541
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10056
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
8447
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
7588
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
6393
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
2
4720
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.