Check whether a HTML page exists
I want to check whether a HTML page exists on another site. More exactly, my users need to upload a .html
page onto their own site and after that they need to press "verify now" on my site. My site then needs to confirm that the html page exists on their site.
I found this code:
$url = 'http://domain.tld/my_page.html';
$handle = @fopen($url,'r');
if($handle !== false){
echo 'Page Exists';
} else {
echo 'Page Not Found';
}
But if my_page.html
don't exist, this cod开发者_运维技巧e returns just "Page Exists".
EDIT 1: sorry fo edditing... do not get the question an first.
you need to understand that server will answer in any way. you just need to check then responsed code, not response text. or to parse text.
in your case server answer like follow:
Object not found!
The requested URL was not found on this server. If you entered the URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
work.local
Thu Jul 28 13:22:49 2011
Apache/2.2.15 (Linux/SUSE)
Edit 2 : you may want better to use socket_connect instead of fopen to check if the file exist
Assuming that you have the curl
extension installed in your PHP, I think this by far your best option:
Easy way to test a URL for 404 in PHP?
A webserver returns the 404 HTTP status code if a resource does not exists. Resource is the html file in your question.
You can do a head request to the server to learn about the status - or just a get request.
PHP has a function build in for this operation as well, it's called get_headers
(Demo):
$headers = get_headers($url, 1);
$statusLine = $headers[0];
list(,$statusCode) = explode(' ', $statusLine, 3);
精彩评论