URL prüfen

Diese Funktion habe ich auf der Seite www.datareise.de gefunden.

Aufruf mit

if(urlValidate('http://www.meine_Domain.de'))
 	{
  		echo "Domain existiert";
 	}
else
	{
  		echo "Domain existiert nicht";
 	}

Funktion URL prüfen

function urlValidate($url)
	{
	    $url = trim($url);
	
	    if(preg_match('%^(?:(?:https?)://)(?:\S+(?::\S*)?@|\d{1,3}(?:\.\d{1,3}){3}|(?:(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)(?:\.(?:[a-z\d\x{00a1}-\x{ffff}]+-?)*[a-z\d\x{00a1}-\x{ffff}]+)*(?:\.[a-z\x{00a1}-\x{ffff}]{2,6}))(?::\d+)?(?:[^\s]*)?$%iu', $url))
		    {
		        if(ini_get('allow_url_fopen'))
			        {
			            $headers = @get_headers($url); 
			            if (preg_match('/^HTTP\/.*\s+(200|301|302)/', $headers[0]))
				            {
				                return true; 
				            }
			        }
		        elseif (function_exists('curl_version'))
			        {
			            $user_agent = "Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:16.0) Gecko/20121026 Firefox/16.0";
			
			            $ch = @curl_init($url);
			            @curl_setopt($ch, CURLOPT_HEADER, 1);
			            @curl_setopt($ch, CURLOPT_NOBODY, 1);
			            @curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 0);
			            @curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
			            @curl_setopt($ch, CURLOPT_HEADER, 1);
			            @curl_setopt($ch, CURLOPT_TIMEOUT, 5);
			            @curl_setopt($ch, CURLOPT_USERAGENT, $user_agent);
			            @curl_exec($ch);
			            $http_statuscode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
			
			            if(preg_match('/^200|301|302$/', $http_statuscode))
				            {
				                return true;
				            }
			        }
		        else
			        {
			            throw new Exception('curl and allow_url_fopen are not avaiable.');
			        }
		    }

    	return false;
	}