Preparing A URL With PHP

There might be many instances where you will create a program in PHP that takes a URL as input and does something with the address. This might be a site analysis or an image resize, but whatever the use is, you need to be sure that the URL will work or at least has the same format.

What users tend to leave out of a URL string is the http:// bit at the start. You could validate the URL to force the user to do this, but you will end up annoying a few people. By far the best way of making sure that the URL has the http:// bit at the start is by adding it behind the scenes. The best way to this is to remove the http:// from the start of the string, even if it isn't there and then add it back on.

$url = str_replace('http://', '',$url);
$url = 'http://'.$url;

This way you absolutely ensure that http:// is there, regardless of whether the user entered it, and you don't have to use any complicated substr()/strpos() combinations to figure out what the URL looks like. Of course this doesn't account for the https protocol, but you can test for this by treating the string as an array.

if ($url[4].$url[5].$url[6] == 's:/') {
 // https
}

Once you have the URL prepared there is no guarantee that the URL is actually an address to a viable resource. To check this you can use the PHP5 function get_headers() to check that the resource returns a valid resource code. The get_headers() function will return an array containing the headers returned from the URL. The first item in the array is always the response code, so if the URL is valid you will get a response of HTTP/1.1 200 OK, so you just have to check for this.

One small note of interest is that the get_headers() call can have a small delay and it doesn't really understand locations that redirect to a difference source so it might not work all of the time. However, if it doesn't work then the chances are that your script won't work either!

Add new comment

The content of this field is kept private and will not be shown publicly.
CAPTCHA
14 + 1 =
Solve this simple math problem and enter the result. E.g. for 1+3, enter 4.
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.