View Single Post
Old 04-11-2007, 12:52 PM   · #1
SecondVersion
while ($awake){ code(); }
 
SecondVersion's Avatar
 
Name: Eric
Location: Kentucky
Trader Rating: (137)
Join Date: Mar 2005
Posts: 4,116
NP$: 259.00 (Donate)
SecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond reputeSecondVersion has a reputation beyond repute
Member of the Month
MOTM September 2005 Save a Life Child Abuse 9/11/01 :: Never Forget Baby Health Marrow Donor Program AIDS/HIV Breast Cancer Cystic Fibrosis Ethan Allen Fund Animal Cruelty Ethan Allen Fund Ethan Allen Fund Cancer Alzheimer's Protect Our Planet
Check if a URL exists.

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.

EDIT: 3/4/2008, updated to more efficient.

PHP 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'] : '/';
    
$path .= (isset($url['query'])) ? "?$url[query]" : '';

    if (isset(
$url['host']) AND $url['host'] != gethostbyname($url['host']))
    {
        if (
PHP_VERSION >= 5)
        {
            
$headers = implode('', 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);
        }
        return (bool)
preg_match('#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers);
    }
    return
false;
}

?>


Please register or log-in into NamePros to hide ads
__________________
SecondVersion.com - The Personal Blog of SecondVersion
Domain Name Portfolio - Get your free copy. - Version 1.0.1 now available!!
MetaCreator.com - Free Meta Tag Creator
SecondVersion is offline   Reply With Quote
Site Sponsors
Hunting Moon Hunting Moon Arcade Script
Advertise your business at NamePros
All times are GMT -7. The time now is 09:41 PM.


Powered by: vBulletin Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.