1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

[PHP] Check if a URL exists

Discussion in 'PHP' started by SecondV, May 9, 2007.

  1. #1
    Howdy.

    This simple function will check if a url is valid (going by parse_url()) and if it's 'online' - by seeing if it returns a 302, 301, or 200 status code.

    
    <?php
    
    function is_valid_url($url)
    {
        $url = @parse_url($url);
    
        if (!$url)
        {
            return false;
        }
    
        $url = array_map('trim', $url);
        $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
        $path = (isset($url['path'])) ? $url['path'] : '';
    
        if ($path == '')
        {
            $path = '/';
        }
    
        $path .= (isset($url['query'])) ? "?$url[query]" : '';
    
        if (isset($url['host']) AND $url['host'] != gethostbyname($url['host']))
        {
            if (PHP_VERSION >= 5)
            {
                $headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
            }
            else
            {
                $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
    
                if (!$fp)
                {
                    return false;
                }
                fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
                $headers = fread($fp, 4096);
                fclose($fp);
            }
            $headers = (is_array($headers)) ? implode("\n", $headers) : $headers;
            return (bool)preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
        }
        return false;
    }
    
    ?>
    
    PHP:
    :)
     
    SecondV, May 9, 2007 IP
    sourcer likes this.
  2. CodyRo

    CodyRo Peon

    Messages:
    365
    Likes Received:
    15
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Seems a bit bloated for something that simple, but useful for some people I suppose.
     
    CodyRo, May 9, 2007 IP
  3. koolasia

    koolasia Banned

    Messages:
    1,413
    Likes Received:
    59
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks this is helpful i. yea its useful for some people
     
    koolasia, May 10, 2007 IP
  4. gibex

    gibex Active Member

    Messages:
    1,060
    Likes Received:
    21
    Best Answers:
    0
    Trophy Points:
    95
    #4
    good function
     
    gibex, May 10, 2007 IP
  5. SecondV

    SecondV Active Member

    Messages:
    76
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    70
    #5
    Glad ya like it. :D
     
    SecondV, May 11, 2007 IP
  6. sourcer

    sourcer Well-Known Member

    Messages:
    960
    Likes Received:
    42
    Best Answers:
    0
    Trophy Points:
    120
    #6
    Thanks for this code, I think I might use it ;) Rep added
     
    sourcer, May 11, 2007 IP