Check url

Wednesday, March 17th, 2010

This is a function that checks to see if a url exists.
Not mine btw, but for the life of me I can’t remember where I got it.

  1. // Check if a url exists
  2. function isValidUrl($url){
  3.                 $url = @parse_url($url);
  4.  
  5.                 if ( ! $url) {
  6.                         return false;
  7.                 }
  8.  
  9.                 $url = array_map('trim', $url);
  10.                 $url['port'] = (!isset($url['port'])) ? 80 : (int)$url['port'];
  11.                 $path = (isset($url['path'])) ? $url['path'] : '';
  12.  
  13.                 if ($path == '')
  14.                 {
  15.                         $path = '/';
  16.                 }
  17.  
  18.                 $path .= ( isset ( $url['query'] ) ) ? "?$url[query]" : '';
  19.  
  20.                 if ( isset ( $url['host'] ) AND $url['host'] != gethostbyname ( $url['host'] ) )
  21.                 {
  22.                         if ( PHP_VERSION >= 5 )
  23.                         {
  24.                                 $headers = get_headers("$url[scheme]://$url[host]:$url[port]$path");
  25.                         }
  26.                         else
  27.                         {
  28.                                 $fp = fsockopen($url['host'], $url['port'], $errno, $errstr, 30);
  29.  
  30.                                 if ( ! $fp )
  31.                                 {
  32.                                         return false;
  33.                                 }
  34.                                 fputs($fp, "HEAD $path HTTP/1.1\r\nHost: $url[host]\r\n\r\n");
  35.                                 $headers = fread ( $fp, 128 );
  36.                                 fclose ( $fp );
  37.                         }
  38.                         $headers = ( is_array ( $headers ) ) ? implode ( "\n", $headers ) : $headers;
  39.                         return ( bool ) preg_match ( '#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers );
  40.                 }
  41.                 return false;
  42. }

Display clean php code for copying

// Check if a url exists
function isValidUrl($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, 128 );
				fclose ( $fp );
			}
			$headers = ( is_array ( $headers ) ) ? implode ( "\n", $headers ) : $headers;
			return ( bool ) preg_match ( '#^HTTP/.*\s+[(200|301|302)]+\s#i', $headers );
		}
		return false;
}