Greetings all,
One of the forms I'm making for my site has a field where someone can enter their web site. Is there anyway that PHP could check for the existence of that site? Somehow fetch the site and if the headers for the site show that its an error (404, for example) then display an error message to the user?
Thanks!
David Goldfeder
Web and Database Specialist
University of Illinois, Urbana-Champaign
Department of Mechanical and Industrial Engineering
Does the web page they entered exist?? Can PHP tell me
Moderator: General Moderators
there are a couple of options to have that done:
- use [php_man]fsockopen[/php_man] to communicate to the webserver hosting that page. It does not require any extensions, but considerable amount of code is needed.
- use [php_man]curl[/php_man] extension.
- if fopen url wrappers are enabled you can check if certain page exists like this:
Code: Select all
function page_exists($page) { $fp = fopen($page, 'r'); fclose($fp); foreach($http_response_header as $header) if(preg_match('%^HTTP/\d\.\d 200%i', $header)) return true; return false; } var_dump(page_exists('http://google.com'));
]Weirdan wrote:if fopen url wrappers are enabled you can check if certain page exists like this:[/list]Code: Select all
function page_exists($page) { $fp = fopen($page, 'r'); fclose($fp); foreach($http_response_header as $header) if(preg_match('%^HTTP/\d\.\d 200%i', $header)) return true; return false; } var_dump(page_exists('http://google.com'));
That'll do. Never knew you could do that. So many settings!!
Edit: Where does the $http_response_header array come from, though? If I answer my own question, I'll post below so you know not to worry about it.
Thanks a bunch!
David Goldfeder
Web and Database Specialist
University of Illinois, Urbana-Champaign
Department of Mechanical and Industrial Engineering
It's PHP black magic in actiondashifen wrote: Edit: Where does the $http_response_header array come from, though? If I answer my own question, I'll post below so you know not to worry about it.
PHP Manual wrote: The stream allows access to the body of the resource; the headers are stored in the $http_response_header variable. Since PHP 4.3.0, the headers are available using stream_get_meta_data().