Archive for July, 2009

Use Your Address Bar Like a Command Line

Monday, July 20th, 2009

There have been many debates back and forth between the development communities as to how your URLs should look. 99% of the time, you are going to find that it is fueled by whatever Mr. Google says to do. That is just fine for ramping up your SEO results, but what about for your applications?

While I am not about to offer any new discipline or technique that you haven’t already heard before, I may offer way for you to approach a very common URL technique that is new to you. The good folks over at the Symfony Project offer, what I feel, to be a very logical method of creating the routes to your web application.

How many times have you seen a url like this …

http://www.example.com/user/432

…and how many times have you tried to change that number to look up a different user?

That is the theory behind using your address bar like a command line. There is no harm in guessing what value should be in there. We build URLs intuitively, and they should be used intuitively. Most people from the Linux community will agree, if we don’t have to use our mouse, we won’t. So if I see I can see how an application is creating it’s URLs, I will use this to my advantage, and it’s foolish to think that your visitors won’t if they are a little tech-savvy.

Take a look at this quote from a tutorial directly from Symfony:

In a web context, a URL is the unique identifier of a web resource. When you go to a URL, you ask the browser to fetch a resource identified by that URL. So, as the URL is the interface between the website and the user, it must convey some meaningful information about the resource it references. But “traditional” URLs do not really describe the resource, they expose the internal structure of the application. The user does not care that your website is developed with the PHP language or that the job has a certain identifier in the database. Exposing the internal workings of your application is also quite bad as far as security is concerned: What if the user tries to guess the URL for resources he does not have access to? Sure, the developer must secure them the proper way, but you’d better hide sensitive information

Couldn’t agree more.

How To Implement

There is no magic potion here, so this will be more of a resource for people looking to learn by example.

Let’s say your application is for movie theaters, and has one functionality of fetching a theater listing by state. A logic approach would be to build your URLs as example.com/theater-list/[state abbreviation]. So if I live close to the border of New Jersey and New York (completely hypothetical, of couse) I can look up example.com/theater-list/nj to find all of the theaters in New Jersey. Then, when I decided I’d rather go to the Imax theather in New York, I can switch it to example.com/theater-list/ny . Sure there is a link on the page to select New York, but typing is just that much quicker –and that means improvements in “click”-thru’s.

Step one involves creating an httpd.conf (or .htaccess) directive to force any request to be routed to index.php.

1
2
3
4
5
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

Now when you try to fetch example.com/theater-list/nj, it will still load index.php, but you can parse out th request URI to call an apporiate file and load specific data.

Request URI = theater-list/nj
$Page_Identifier = theater-list (maybe use it in a switch … and let’s say, call theater-list.php)
$Data_Identifier = nj

So now inside of index.php

1
require_once ($page_indentifier); // which is a file that we indentified using "theater-list"

Then, inside of theater-list.php, do something like

1
$sql = "SELECT * FROM theater-list WHERE state = '". $Data_Identifier . "' // of course, you will cleanse this data

Posted in Lecture | No Comments »

Set a value of 0 for unchecked checkboxes

Thursday, July 9th, 2009

Many people who aren’t too familiar with the Ruby on Rails framework may do well to learn about a convenient little trick they do with submitting check boxes.

In HTML, when you click a check box and submit it, by default, the value POSTed is “1″ – and if you do not click it, nothing is submitted. This sometimes makes keeping track of check boxes difficult.

To combat this, Rails puts a hidden field before the check box, with the same name as the check box, with a value of zero.


Click this to set my_ckbx to 1

Now when the form loads, my_chbx is defaulted to “0″. If you choose to click the check box, my_ckbx will be set to “1″, if not, it will remain “0″. No matter what, my_ckbx will POST a value.

Tags:
Posted in Code | No Comments »