An ID obfuscation function that fits into a Tweet

If you’re bothered about long IDs in your app or want to shorten existing large number keys, there are many examples on the web that take your numeric ID and perform some witchcraft with a bunch of numbers, letters and a database to turn something like http://www.mydirtysocks.com into 3Q8zk. What’s basically happening is that the URL is stored in a database and a unique key is generated for it (usually by using the ID field of the table). Some services check if the URL already exists and, if it does, returns the existing key.

When someone visits the shortening service with the key, the serivce looks it up and redirects you to the original URL.

The basic premise of shortening is that once you’ve exhausted numbers 0 – 9, you then move on to a – z and then A – Z. So instead of a character pool of just 36 with numbers + lower case letters, you now have a pool of 62 to represent a long ID. This is almost never why people use these functions and most don’t care that their ID is shorter.

What they’re really using it is for hiding that ID 10001 came before 10002 and for this, a lot of the shortening mechanisms out there are severely overkill. And then there’s the fact that because you’re using both upper and lower case letters; if some genius decided to turn all uppercase letters in a URL to lowercase in their forum/blog/email-service or some other kitchen sink application, your whole shortening scheme is hosed. This happens far more often than you think.

If you just wanted to generate a unique key (provided the original ID was unique) instead of showing the original ID, then there’s an alternative that’s exactly 140 characters. I checked :

All this does is take that initial ID in string format, split it into 9 character chunks, generate a CRC32 hash of each chunk, convert it to base 32 (the maximum value allowed for the base_covnert function in PHP) and append to the output.

Of course, if you want a more nice-ified version of that…

function shortenID($k){

	$out='';
	$p = str_split($k,9);
	
	for( $i = 0; $i < count( $p ); $i++ ) {
		$out. = base_convert( crc32( $p[$i] ), 16, 36 );
	}
	
	return $out;
}

Now, generating the ID is only half of the implementation. Since the hash cannot be reversed, you should generate it once for that particular ID and store it in a separate field in the same row of the DB table. This is what almost all URL shortening services do, but of course, they use that upper and lower case character malarkey.

Of course, if you do still want that malarkey, Lalit has created a class that does it too. Stop searching now and go download that class.

However…

Please try to be sane with your URL shortening shenanigans. It’s really quite stupid to shorten an already short URL, not to mention very irritating.

Advertisement

Turn off Firefox3 address bar bookmark search.

Firefox address bar Bookmarks = Incredibly Annoying!

I’m sure there are some users who find this useful, but considering I have, literally, a hundred+ bookmarks and I get to see this every time I enter in any character, it’s really annoying.

For those of you who don’t know what I’m talking about…

Well intentioned, but annoying for me

Well intentioned, but annoying for me

I keep all of them organized into folders so I always find what I’m looking for. This was really unnecessary for me.

So how to turn it off?

Go to your about config > search for urlbar > select browser.urlbar.matchOnlyTyped and set it to true.

config, find browser.urlbar.matchOnlyTyped and set it to true

In about:config, find browser.urlbar.matchOnlyTyped and set it to true

There… Problem solved!

Like I said before, there are plenty of people who would find this useful. But I’m not one of them. I’m sure a lot of others out there aren’t either.

Update

On second thought, that number of a hundred+ bookmarks may be slightly off…

Here is a list of those folders..

I didn't really take a moment to glance at this

I didn’t really take a moment to glance at this

And a few samples within… Yes, there are folders within folders

Design related bookmarks

Design related bookmarks


Electronics related bookmarks

Electronics related bookmarks

Upon closer inspection, the actual number may be several hundred to a thousand bookmarks.
Each folder has about the same number of links in them… Yikes!