Does the web page they entered exist?? Can PHP tell me

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

Does the web page they entered exist?? Can PHP tell me

Post by dashifen »

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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

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'));
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

Post by dashifen »

Weirdan wrote: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'));
[/list]
]

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
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Post by Weirdan »

dashifen 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.
It's PHP black magic in action :lol:
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().
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

Post by dashifen »

Weirdan wrote:It's PHP black magic in action :lol:
Nice. Thanks again.
Post Reply