Creating randomized valid file paths is a common requirement for many applications such as the case of short url redirects. The Goo.gl url shortening service produces a random Alphanumeric file path 5 characters long. This allows letters A~z and decimal numbers 0~9. Five alphanumeric character ought to be enough in practice for most applications as this equates to 62^5 = 1 Billion (916,132,832) unique url combinations before you need to revisit the situation or run into a y2k wall of sorts.

For example, a short url may look something like this:

http://goo.gl/XMoug

Behind the scenes, this url is then redirected by the service to its proper destination such as http://wayne-doucette.blogspot.com/

Here's a function in PHP that returns an ASCII character array, defaulting to 5 characters in length. -Although the forward slash is a permissible character, I have not allowed it in my application as I do not wish the generated paths to represent file directories.

getHash() first loads a decimal array representing ASCII codes of the permissible character set, seeds the PHP random number generator, then finally builds and returns a random array of ASCII characters.

To implement the method, simply do something like this:

0 comments:

Dynamic Page QR Code

Popular Posts

My LinkedIn PingTag


View My Stats