IT.COM

Check if a URL exists.

NameSilo
Watch
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:
<?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;
}

?>
 
Last edited:
0
•••
The views expressed on this page by users and staff are their own, not those of NamePros.
I needed this a little while ago, and here is what I have:
PHP:
function url_exists($url) {
@ $headers = get_headers($url);
    return preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/', $headers[0]);
}
then use:
if ($headers[0] == 1) {
code
}
 
0
•••
bobby9101 said:
I needed this a little while ago, and here is what I have:
PHP:
function url_exists($url) {
@ $headers = get_headers($url);
    return preg_match('/^HTTP\/\d\.\d\s+(200|301|302)/', $headers[0]);
}
then use:
if ($headers[0] == 1) {
code
}
Not everyone has PHP5 ;)
 
0
•••
Updated the code, check first post.
 
0
•••
SecondVersion said:
Updated the code, check first post.

I don't get it, how to use that code above? sorry, im a newbie with php
 
0
•••
abcde said:
I don't get it, how to use that code above? sorry, im a newbie with php

First, you need a webhost which supports PHP or to install it on your own PC. But on your own PC you need to also install a webserver and it's a real hassle so it's easier to have a webhost with this.

Then, input this script in to a file called file.php (or whatever_you_want.php) and save and close the file.

Now in SecondVersion's script he created the function called $is_valid_url($url); so what you'll want to do to see if a URL exists: (within the same file that the function is in [or if it's included for that matter])

PHP:
<?php
// Test script. Function created by SecondVersion
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;
}

if(is_valid_url("http://namepros.com"))
{
	echo("w00t for Namepros.com! It's alive!");
}else{
	echo("Oh no! Where did NamePros go!?! :(");
}

?>

-RageD
 
0
•••
Thanks for answering that for me, RageD. :tu:
 
0
•••
why not just:

$var = file_get_contents($url);
if ($var == TRUE) {
// url is ok
} else {
// url is invalid
}
 
0
•••
itnashvillecom said:
why not just:

$var = file_get_contents($url);
if ($var == TRUE) {
// url is ok
} else {
// url is invalid
}
What would be the point of getting the URL's content? That'd be useless and inefficient. This function does the same without the burden of gathering content.
 
0
•••
Just set max length to 1 to keep from grabbing the whole thing.. Beats using a gigantic function, overkill to me. Much less processing time and lower load.
 
Last edited:
0
•••
  • The sidebar remains visible by scrolling at a speed relative to the page’s height.
Back