Tag Archives: url

PHP: A Quick Way to Break up a URL into usable Pieces CodeUnit 27 JAN 2012

PHP has a built in function which allows for the quick breaking up of a well formed URL into usable pieces, namely parse_url.

This function parses a URL and returns an associative array containing any of the various components of the URL that are present. It is important to note that parse_url does not validate the given URL, it simply breaks it up into parts. Partial URLs are also accepted, and whilst parse_url tries its best to parse them correctly, it may return -1 if the URL provided is too malformed.

So what exactly does it break an URL up into then?

Well, given an URL like below, parse_url would return the following:

$url = 'http://username:password@hostname/path?arg=value#anchor';


echo parse_url($url, PHP_URL_PATH);

The output:

    [scheme] => http
    [host] => hostname
    [user] => username
    [pass] => password
    [path] => /path
    [query] => arg=value
    [fragment] => anchor


So pretty useful if you want say just the host or query value from a given URL then!

Another thing to note is that it is not designed to work with relative URLs, meaning that you do need to provide it with fully qualified URLs in order to get something usable back.

Still, a pretty nifty little function to keep in mind when writing parsing scripts…

Related Link: http://www.php.net/manual/en/function.parse-url.php

WordPress PHP: How to add URL Querystring Parameters to Your Plugin CodeUnit 26 DEC 2011

WordPress is set up such that unknown URL GET parameters are ignored, a nice security/navigation feature to be sure, but an annoying one if you are developing a plugin that needs to allow for GET variables in order to control your output.

Luckily there is a solution to achieve this though… to add new allowed URL parameters to your WordPress site, simply extend the query_vars filter! :)

In practice (you add this at the top of your plugin code):


function wtap_queryvars($qvars){
    return $qvars;

Note that it is a good idea to give your accepted GET parameters unique names so that you won’t clash with already accepted and processed GET variables. In the example above, I’ve gone and prefixed everything with wtap_.

Now that your site allows GET parameters, you obviously need to make use of them. To do this, first check their existence and then grab them via the wp_query global object:

global $wp_query;
if (isset($wp_query->query_vars['wtap_year'])){
echo '<p>Year passed in url: ' . $wp_query->query_vars['wtap_year'] . '</p>';

Great. So now http://yoursite.com/plugin-page/?wtap_year=2011&wtap_month=12 will throw up something valid, instead of an annoying 404 page!


PHP: Test if an Image URL is Online CodeUnit 25 MAY 2011

Here is a quick and dirty simple function to check whether or not an image URL is valid, in other words the image is online, by running a quick get_headers check combined with some REGEX wizardry.

The function returns a simple true if the image/URL is active and false if it is not (i.e. the server returns a 404).

function url_exists($url) {
    $hdrs = @get_headers($url);
    return is_array($hdrs) ? preg_match('/^HTTP\/\d+\.\d+\s+2\d\d\s+.*$/',$hdrs[0]) : false;


When Your Zend Framework Project returns Page Not Found Errors but Everything is Configured Correctly CodeUnit 29 APR 2011

It can be pretty frustrating. Your Zend Framework project is configured 100% correctly. Mod Rewrite is enabled and working on your server, the .htaccess file is configured correctly and working, all your controllers are correctly created and specified, all with the necessary index actions, your modules are healthy, in short everything is looking 100% like all the examples taught you. Yet for some reason when you try and browse your site, you keep getting Page Not Found, Controller not specified error messages!

I had this issue recently and it was causing me to tear my hair out in frustration. My project was modularized and I was using a menu in the layout, defined in the application.ini file. Everything looked correct and on my Windows dev box all navigation seemed to be working correctly. However, copy the code over to the Ubuntu server and the navigation suddenly broke. Much frustration later, I eventually twigged as to what was wrong. The URLs contained in my menu.

As we all know, camelCase is the default way to express controller and action names in Zend. However, what we do sometimes forget, is that the default URLs are expressed in lowercase, with hyphens indicating the start of a uppercase letter.

In other words while http://myproject/ViewLogs/ may look right to you at first glance, because it matches the name of the controller perfectly, the correct URL is actually http://myproject/view-logs/.

If you remember that, then you shouldn’t go wrong. (Wish I had, to be honest).

So the simple fix was to change the way I specified my menu, from:

resources.navigation.pages.oslo.pages.sub.label = "Allowed Access"
resources.navigation.pages.oslo.pages.sub.module = "oslo"
resources.navigation.pages.oslo.pages.sub.controller = "AllowedAccess"
resources.navigation.pages.oslo.pages.sub.action = "index"


resources.navigation.pages.oslo.pages.sub.label = "Allowed Access"
resources.navigation.pages.oslo.pages.sub.module = "oslo"
resources.navigation.pages.oslo.pages.sub.controller = "allowed-access"
resources.navigation.pages.oslo.pages.sub.action = "index"

And now you know.

How to Convert a Webpage to a PDF CodeUnit 15 FEB 2010

Saving a web page to a PDF file for later offline viewing has just become a whole lot simpler thanks to the excellent pdfmyurl.com web service that aims to convert any URL you feed it into a downloadable PDF file. The application is particularly easy to use, looks good and returns some excellent results, making it the perfect tool for a beginner web user.

However, its magic doesn’t quite just end there.

Equipped with a host of settable options like layout, page size, dpi and zoom factor for example, pdfmyurl is geared towards allowing other developers to use the service either by making calls to it via an href anchor with the associated GET variables set, or even on their own servers if for instance they wanted to make use of the brilliant wgets functionality in order to rip a website down into PDF format.

For example:

wget -O opentracker.pdf "pdfmyurl.com?url=www.opentracker.net&-O=Landscape&--header-left=hello"

would result in the system generating a pdf file entitled opentracker.pdf in a landscape orientation and with a custom header text.

Particularly interesting for this system is the way it manages to keep menus and links active in the generated PDF file, meaning that should the page you saved as a PDF reference another article, you could still access that referenced article at a later stage by clicking on the link contained in the PDF file.

In other words, this whole damn system is simply put, pretty nifty! ;)

Related Link: http://www.pdfmyurl.com

Shorten Your URLs the Bit.ly Way! CodeUnit 29 JAN 2010

URL shorteners are all the rage at the moment, made particularly popular by the advent of micro-blogging platforms like Twitter and even FaceBook updates for that matter. The concept is pretty simple. Take a long URL, pass it on to the shortening service and receive a far more compact URL that points to the original URL.

Obviously you can see the advantage of this.

Now I must admit that my favourite shortener at the moment (and believe me, there are many of these services out there) is bit.ly.

Aptly support by its bright blowfish mascot, bit.ly performs the basic URL shortening function quickly and with minimal effort, but at the same time enhances the experience with a friendly, quick and quite usable user interface.

On top of all this, they then throw in a whole lot of extra services like tracking shortened URL clicks, storing your converted URL history, and registering a host of other stats against all manner of shortened URLs, never mind all the goodies if you upgrade to a Pro account.

Pro account? Yup, believe it or not, they’ve even managed to figure a way of monetizing this particularly simple concept!

In any event, what I really want to say is that they offer a pretty damn good service – even if it is something as simple as shortening your URLs for you! ;)

Related Link: http://bit.ly