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

how to validate a url

P: n/a
Hi,

Anyone can tell me how to validate
if a web page is online or 404 ?

Thanx

Yang

--
-------------------------------------------------------------------------
FIGHT BACK AGAINST SPAM!
Download Spam Inspector, the Award Winning Anti-Spam Filter
http://mail.giantcompany.com

Jul 16 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Erwin Moller <si******************************************@spam yourself.com> writes:
Yang Li Ke wrote:
Hi,

Anyone can tell me how to validate
if a web page is online or 404 ?


A portable an reliable method :

404 :
02/09 16:22 root@fusion ~# telnet localhost 80 Err 1 #16
#I send :
HEAD /does_not_exists.php HTTP/1.0

#I receive
HTTP/1.1 404 Not Found
Date: Tue, 02 Sep 2003 14:24:04 GMT
Server: Apache/1.3.26 (Unix) PHP/4.3.3 mod_ssl/2.8.10 OpenSSL/0.9.6g
Connection: close
Content-Type: text/html; charset=iso-8859-1

online :
#I send :
HEAD / HTTP/1.0

#Ireceive :
HTTP/1.1 200 OK
Date: Tue, 02 Sep 2003 14:25:03 GMT
Server: Apache/1.3.26 (Unix) PHP/4.3.3 mod_ssl/2.8.10 OpenSSL/0.9.6g
Content-Location: index.html.en
Vary: negotiate,accept-language,accept-charset
TCN: choice
Last-Modified: Wed, 29 Jan 2003 10:58:19 GMT
ETag: "381a2-a71-3e37b3cb;3f546ad9"
Accept-Ranges: bytes
Content-Length: 2673
Connection: close
Content-Type: text/html
Content-Language: en
Expires: Tue, 02 Sep 2003 14:25:03 GMT

So, You can easily use it with the help of fopen(), fputs(), etc....

Good luck.

--
Julien CROUZET

Jul 16 '05 #2

P: n/a
Yang Li Ke, still there??

What Julien suggested is maybe not working.
Here is a little script of my, and it does not output the headers.

Julien, do you know why I do not receive any headers this way??

---------------------------------
<?
$theurl = @$_POST["theurl"];
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>urltest</title>
</head>
<body>

Check any URL:
<form action="" method="POST" name="urlform">
<input type="hidden" name="getdata" value="Y">
<br>
<input type=text size=40 name="theurl" value="<? echo $theurl; ?>">
<br>
<input type="submit" value="haal data">
</form>

<?
if (isset($_POST["getdata"])) {
// okay get the url
// only works if track_errors = On is set in php.ini
$php_errormsg = "";
$handle = @fopen ($theurl, "r");
if ($php_errormsg != "") {
?>
Cannot open URL....
<?
} else
{

?>
<hr>
<b>The content of <i>$theurl</i>:</b>
<br>
$php_errormsg= <? echo $php_errormsg; ?>
<hr>
<pre>
<?
while (!feof ($handle)) {
$buffer = fgets($handle, 4096);
echo htmlentities($buffer);
}
fclose ($handle);
ECHO "</pre>";
}
}
?>

</body>
</html>

Jul 16 '05 #3

P: n/a
Erwin Moller <si******************************************@spam yourself.com> writes:
Yang Li Ke, still there??

What Julien suggested is maybe not working.
Here is a little script of my, and it does not output the headers.

Julien, do you know why I do not receive any headers this way??

---------------------------------
<?
$theurl = @$_POST["theurl"];
?>

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0//EN">
<html>
<head>
<title>urltest</title>
</head>
<body>

Check any URL:
<form action="" method="POST" name="urlform">
<input type="hidden" name="getdata" value="Y">
<br>
<input type=text size=40 name="theurl" value="<? echo $theurl; ?>">
<br>
<input type="submit" value="haal data">
</form>

<?
if (isset($_POST["getdata"])) {
// okay get the url
// only works if track_errors = On is set in php.ini
$php_errormsg = "";
$handle = @fopen ($theurl, "r");
Here, you open the socket
if ($php_errormsg != "") {
?>
Cannot open URL....
<?
} else
{

?>
<hr>
<b>The content of <i>$theurl</i>:</b>
<br>
$php_errormsg= <? echo $php_errormsg; ?>
<hr>
<pre>
<?
while (!feof ($handle)) {
$buffer = fgets($handle, 4096);
here you read the socket till the server close the connection
echo htmlentities($buffer);
}
fclose ($handle);
ECHO "</pre>";
}
}
?>

</body>
</html>


You must send "HEAD /page.php HTTP/1.0\n\n" to have an answer.

And be careful of "while (!feof(" cause sometimes apache
doesn't close the connection after the answer, and wait for
another query during {x} seconds.

Close you socket once you received the status.

Good luck again =)

--
Julien CROUZET
Jul 16 '05 #4

P: n/a
"Yang Li Ke" <ya******@sympatico.ca> writes:
>$php_errormsg = "";
>$handle = @fopen ($theurl, "r");
>if ($php_errormsg != "") {


I dont really understand how will the $php_errormsg get a value if u dont
put any $php_errormsg = something;


"The previous error message: $php_errormsg
$php_errormsg is a variable containing the text of the last error
message generated by PHP. This variable will only be available within
the scope in which the error occurred, and only if the track_errors
configuration option is turned on (it defaults to off)."

It is a reserved variable.

--
Julien CROUZET

Jul 16 '05 #5

P: n/a
Thanks for explaining Julien.
:-)
Jul 16 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.