How to Validate a URL with PHP


Categories
Tags
A simple function using PHP and CURL that checks whether a URL is valid or not, by returning true or false.

This function has been adapted from a response to a similar question on Stack Overflow.

If you’re using WordPress this function can be added to functions.php. The function accepts a passed parameter (the URL) and checks if it is valid or not, then returns a true or false value depending on the outcome:

function validate_url($url){
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_exec($ch);
	$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

	if ($code == 200) {
		$status = true;
	} else {
		$status = false;
	}
	curl_close($ch);
	return $status;
}

If you’re using WordPress and want to check if a media file on your site is valid, you might want to check if the URL is relative first, and prefix it with your site URL if it is. See adapted version below which will check this first:

function validate_url($url){
	//check if media url is relative (starts with '/wp-content'), if so append site url so we have a full url
	if( substr( $url, 0, 11 ) === "/wp-content" ){ //0,11 represents 1st and last char of the string
		$site_url = site_url();
		$url = $site_url .= $url;
	}
	//if url is valid return true, else false
	$ch = curl_init($url);
	curl_setopt($ch, CURLOPT_NOBODY, true);
	curl_exec($ch);
	$code = curl_getinfo($ch, CURLINFO_HTTP_CODE);

	if ($code == 200) {
		$status = true;
	} else {
		$status = false;
	}
	curl_close($ch);
	return $status;
}

To use the function simply call it with a passed parameter like so:

$url = 'http://dragoncode.co.uk/';
$valid_url = validate_url($url);

Want to do something based on whether it returns true or false? Here’s how:

if(validate_url($url) === true){ //if url is valid...
	//do something (true)
} else
	//do something else (false)
}	

Comments

Whether you have feedback, a question, want to share your opinion, or simply want to say thank you - we welcome comments! Please read the disclaimer.

Disclaimer and Legal Information

The use of any content found on or via this website (code, how to guides etc.) is done so at your own risk. We take no responsibility for any issues encountered.

This blog and its authors will not be held responsible for any misuse, reuse, recycled and cited/uncited copies of content from this website by others.

The views and opinions expressed in this blog are those of the author(s) and do not necessarily reflect the position or opinion of any other agency, organisation, employer or company.

Comments are moderated but we do not take any responsibility for any libel or litigation that results from something written in a comment. We reserve the right to reject or delete any comment for any reason whatsoever (abusive, profane, rude etc). Please keep your comments polite and relevant.

We are happy for you to quote and share our content in any reasonable manner, e.g. post links to our blogs on social media, but not in any way that suggests that we, or our authors, endorse you, your use or your views. Nothing negative please.

We appreciate attributions, e.g. a link to our website (www.dragoncode.co.uk).

Thank you – we hope you find the website useful.