Share tips for keeping URL length short!

Not for 'how-to' coding questions but PHP theory instead, this forum is here for those of us who wish to learn about design aspects of programming with PHP.

Moderator: General Moderators

Post Reply
User avatar
JAB Creations
DevNet Resident
Posts: 2341
Joined: Thu Jan 13, 2005 6:44 pm
Location: Sarasota Florida
Contact:

Share tips for keeping URL length short!

Post by JAB Creations »

My screen's native resolution is 1920x1200 and even though the address bar is 50% of that width I still cringe when I see obscenely long URL's. So I thought I'd start a short URL tips thread so we can learn each other's tactics for keeping URL's reasonably short!

This morning I've been working on the member index page that simply shows a list of registered members on my site. Locally I have error_reporting set to E_ALL as I like forcing myself to code to a higher standard whenever possible. One of stricter warnings with this error mode are undefined variables (e.g. $_GET['something'] is not defined). I was trying to figure out how to get the order URL's to work without generating any PHP warnings and then I realized instead of having the URL look something like members/?column=alias&sort=desc I could simply use members/?alias&desc. This meant instead of first determining if the value was set and then what it was set to...and still having to repeat the same steps (since there are multiple columns to sort by) all I had to do was execute a simple isset to determine if I should echo anything special out.

Here is some example code not yet ready for a live environment though it should give an idea of how simple this approach is. Suggestions for improvement are of course welcomed!

Code: Select all

echo '<thead>'."\n";
echo '<tr><td><a class="block" href="members/';
if (!isset($_GET['alias'])) {echo '?alias&desc';}
echo '" rel="nofollow" tabindex="3" title="">Alias</a></td><td><span>Location</span></td><td><a class="block" href="members/';
if (isset($_GET['posts'])) {if (!isset($_GET['desc'])) {echo '?posts&desc';} else {echo '?posts';}} else {echo '?posts';}
echo '" rel="nofollow" tabindex="3" title="">Posts</a></td><td><a class="block" href="members/';
if (isset($_GET['rank'])) {if (!isset($_GET['desc'])) {echo '?rank&desc';} else {echo '?rank';}} else {echo '?rank';}
echo '" rel="nofollow" tabindex="3" title="">Rank</a></td><td><a class="block" href="members/';
if (isset($_GET['registered'])) {if (!isset($_GET['desc'])) {echo '?registered&desc';} else {echo '?registered';}} else {echo '?registered';}
echo '" rel="nofollow" tabindex="3" title="">Registered</a></td></tr>'."\n";
echo '</thead>'."\n";
I'm curious about what others have done to keep their URL length reasonable?
AlanG
Forum Contributor
Posts: 136
Joined: Wed Jun 10, 2009 1:03 am

Re: Share tips for keeping URL length short!

Post by AlanG »

I don't really have any suggestions regarding the url length as I don't consider it an issue.
I do have a suggestion for your validation however.

Code: Select all

<?php if (!isset($_GET['alias'])) {echo '?alias&desc';} ?>
If ?alias= is all thats entered into the url, the above condition will return true. You should check if the variable is empty also

Code: Select all

<?php if (!isset($_GET['alias']) && !empty($_GET['alias'])) {echo '?alias&desc';} ?>
matthijs
DevNet Master
Posts: 3360
Joined: Thu Oct 06, 2005 3:57 pm

Re: Share tips for keeping URL length short!

Post by matthijs »

Or use clean urls when possible ('/members/jab/'). Or else if you need the query, you can of course use shorter items ('?a=10&b=d')
Post Reply