How to check error 404 in php

Discussion in 'PHP' started by PinoyIto, Sep 18, 2008.

  1. #1
    Hello guys, will you please give me a sample code on how to check if the specified url return 404 error..

    thanks in advance
     
    PinoyIto, Sep 18, 2008 IP
  2. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #2
    CreativeClans, Sep 18, 2008 IP
  3. mehdi

    mehdi Peon

    Messages:
    258
    Likes Received:
    12
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Try this:
    
    <?php
    $site = "http://site.com";
    $get = file_get_contents($site);
    if(preg_match('#404#', $get)){
    echo "This site has 404 Error";
    }else{
    echo "This site doesn't have 404 Error";
    }
    ?>
    
    PHP:
     
    mehdi, Sep 18, 2008 IP
  4. linkexchange1984

    linkexchange1984 Peon

    Messages:
    73
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Creating rule @ .htaccess is best...........here.

    ----------------------------------
    Best website solution @ cheap rates
    www.seowebindia.com
     
    linkexchange1984, Sep 18, 2008 IP
  5. webrickco

    webrickco Active Member

    Messages:
    268
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    60
    #5
    I think he asked for a checking, not for a redirecting rule...
     
    webrickco, Sep 18, 2008 IP
  6. JEET

    JEET Notable Member

    Messages:
    3,832
    Likes Received:
    502
    Best Answers:
    19
    Trophy Points:
    265
    #6
    If using php5 try this:
    <?php
    echo '<pre>'; print_r(get_headers('http://www.google.com')); echo '</pre>';
    ?>

    regards :)
     
    JEET, Sep 18, 2008 IP
  7. CreativeClans

    CreativeClans Peon

    Messages:
    128
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Yes, but it all depends on what he means with a 'specified url'. If it's a url inserted by the user in a form, then you can use PHP to check it. But if it's a url to your site that the user wants to visit (inserted in the address bar of the browser), then you'll have to use .htaccess.
     
    CreativeClans, Sep 19, 2008 IP