Welcome to WebmasterWorld Guest from 3.227.249.234

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Checking if page exists

     
3:31 pm on Dec 13, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 11, 2006
posts:379
votes: 0


Hi. I need a way to check if a specified page on a specified host exists.

Would this work or do i need to do some kind of host check before?

$fp = file_get_contents('http://something.net/something.html');
if($fp)
echo 'ok';
else
echo 'not ok';

3:47 pm on Dec 13, 2006 (gmt 0)

Senior Member

WebmasterWorld Senior Member 10+ Year Member

joined:Aug 21, 2003
posts:1069
votes: 0


It should work as is. I'm guessing that even though the host may not be valid OR the page may not be valid then you would always want a false returned anyway.

JAG

4:29 pm on Dec 13, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 13, 2006
posts:28
votes: 0


I would check the response headers:

if($sock=fsockopen('something.net',80))
{
fputs($sock, "HEAD /something.html HTTP/1.0\r\n\r\n");

while(!feof($sock))
{
echo fgets($sock);
}
}

This will echo 'HTTP/1.0 404 Requested URL not found' if the page does not exist or 'HTTP/1.0 200 OK' if the page does exist.

[edited by: coopster at 5:13 pm (utc) on Dec. 13, 2006]
[edit reason] removed url [/edit]

5:16 pm on Dec 13, 2006 (gmt 0)

Administrator

WebmasterWorld Administrator coopster is a WebmasterWorld Top Contributor of All Time 10+ Year Member

joined:July 31, 2003
posts:12555
votes: 3


Good point, if you don't need the page contents then checking response headers will likely be quicker.

Welcome to WebmasterWorld, mattcg.

2:45 am on Dec 14, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 11, 2006
posts:379
votes: 0


well, actually i found out that i also need some info of the meta tags. so, in that case, i use this code:

$tags = @get_meta_tags('http://123.com/test.php');
print_r($tags);

but still, if the page itself has no meta tags, i still have to check if the page exists.

3:08 am on Dec 14, 2006 (gmt 0)

Preferred Member

10+ Year Member

joined:Mar 11, 2006
posts:379
votes: 0


ok... i've come to this:

1) i have to check if the HOST exists.
2) in case the url has a page or path, i also need to check if they exist.
3) i need to grab the page's meta tags into an array.

For all that i am doing this thanks to the code provided above:


$h = fsockopen('somehost.com',80);
if($h) {
$fp = file_get_contents('http://somehost.com/some.html');
if($fp) {
$tags = get_meta_tags('http://somehost.com/some.html');
print_r($tags);
}
}

----Is that enough, or maybe it could get optimized? Thanks again, Andres.

12:28 am on Dec 15, 2006 (gmt 0)

New User

10+ Year Member

joined:Dec 13, 2006
posts:28
votes: 0


This might just be superficial, but I prefer using "or die()" to "if(isset($var))" e.g.


$h = fsockopen('somehost.com',80)
or die();
$fp = file_get_contents('http://somehost.com/some.html')
or die();
$tags = get_meta_tags('http://somehost.com/some.html');
print_r($tags);

Maybe someone with more experience can confirm whether this is more efficient or not.

4:18 am on Dec 15, 2006 (gmt 0)

Senior Member

joined:Nov 12, 2005
posts:5967
votes: 0


>>>confirm whether this is more efficient or not.

If it is more efficient is by an insignificant amount; it wouldn't even be noticeable, IMO.