Shorter Web Addresses With Variables

How to use variables in web addresses in a much shorter way.

15th December 2004 · Last updated: 5th October 2016
 

Comments


I've recently discovered that you can parse all the variables on the end of a web address in one go. Then simply use PHP to split the string down. What this means is that you can do away with the variable names and any ampersands used to separate them. It's a great trick for shortening the length of the address. Here's exactly what I mean:

Your existing address might be something like this:

file.php?page=intro&style=manilla

This is how I would code it:

file.php?intro-manilla

You can use any symbol you like to separate the variables.

What I've done is scrapped the 'page' and 'style' definitions as you can work those out from the string! Normally you would access the variables like this at the start of the page (in PHP):

$page = $_GET['page'];
$style = $_GET['style'];

You can reference them directly, but this can be a security risk, so it's advised to do it the above way, and set register_globals off in your PHP ini file. (Newer versions of PHP have this off by default.)

Here is the code required to do away with getting the 'page' and 'style' variables completely:

$all = $_SERVER['QUERY_STRING'];

Now we have everything after the "?" at the end of the address, so it's just a matter of splitting the string by using whatever character you defined to separate the variables - in this case, the hyphen:

list ($page, $style) = split ('-', $all);

So now we have a variable $page that equals "intro" and another $style that equals "manilla". Much simpler!

The only drawback would be if you kept swapping the order of the variables around.

Comments (6)

Comments are locked on this topic. Thanks to everyone who posted a comment.
 

  1. Tom Clancy:
    But why do that at all? You force yourself to always provide all of the variables and it seems like it would lead to some SQL injection risks if you were accepting data that way, like your validation would be diluted because you're going to handle everything at once. I know it wouldn't *force* you to dilute the input validation, it just feels like human nature would lead to that.

    Anyways, don't all the cool kids use mod_rewrite (or ISAPI_rewrite on IIS) to make the variables look like URLs?

    Posted on 16 December 2004 at 1:04 am
  2. Blowout:
    Let's not forget to make the webserver recognise the script when we use & or & in the URL!

    Posted on 25 December 2004 at 10:17 pm
  3. wok:
    The issue I see with this method is that you're split character (in this case '-') can no longer be used in any of the parameters. For example, if the intended value being passed is 'wok-balls', it wont be parsed correctly and we'll be cutting 'wok-balls' in half. Please don't cut my balls in half!

    Posted on 26 December 2004 at 3:49 pm
  4. Parsons:
    FIRST OF ALL, I AM AMAZED AT YOUR LAYOUT!!! ITS WIKKID. DO YOU USE HTML? AND PAINTSHOP?.

    I AM ALSO AN EXPERT WEB DESIGNER. I HAVE BEEN USIING PAINTSHOP PRO FOR ALL MY WORK, ITS THE BEST PROGRAM I THINK.

    ANYONE KNOW WHERE I CAN GET VERSION 4? I NEED TO UPGRADE...

    THX IN ADVANCE!

    Posted on 26 December 2004 at 4:00 pm
  5. Chris Hester:
    Wok, the character used to split the variables can be anything that can go in a URL. If you need a hyphen, use something else like '@' or even '&'. If that's not possible you could set up a rule such as if there are two hyphens in a row, replace them with a single hyphen, but don't split the string. (A way to 'escape' the hyphen, like the slash in PHP.)

    Why not use a dot instead? That would also work. Eg:

    wok.balls

    Posted on 26 December 2004 at 10:22 pm
  6. Dean:
    I believe the W3C recommend semi-colons as their preferred delimiter. I've toyed with the idea of using '/' as the delimiter for vars. You can use http://example.com/index.php/de/12345/artikel_ueber_nix for example, which looks like a normal url and you will find /de/12345/artikel_ueber_nix in $_SERVER['PATH_INFO'] ready for parsing. You can add the ? and GET vars as normal after this. It's a nice way to pass one or two variables and gives a nice search engine-friendly URL.

    Posted on 31 January 2005 at 10:00 am