Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check url if valid
#1
Credits and Source: http://www.roscripts.com/

Name: Check url if valid
Description: Returns true/false if a given url is alive or not.
Snippet:

PHP Code:
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 >= )
            {
                
$headers get_headers("$url[scheme]://$url[host]:$url[port]$path");
            }
            else
            {
                
$fp fsockopen($url['host'], $url['port'], $errno$errstr30);

                if ( ! 
$fp )
                {
                    return 
false;
                }
                
fputs($fp"HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
                
$headers fread $fp128 );
                
fclose $fp );
            }
            
$headers = ( is_array $headers ) ) ? implode "\n"$headers ) : $headers;
            return ( bool ) 
preg_match '#^HTTP/.*\s+[(200|301|302)]+\s#i'$headers );
        }
        return 
false;



Thankyou for reading. Be happy always Smile
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Shorten URL zone 0 552 11-08-2009, 08:46 AM
Last Post: zone

Forum Jump:


Users browsing this thread: 1 Guest(s)