|Welcome to the Monastery|
Re: Long url to tiny url.by RMGir (Prior)
|on Jul 07, 2007 at 09:38 UTC||Need Help??|
There are a lot of different ways it could be done.
But mainly, all you need is a database with 2 fields: realURL and tinyURL.
When someone submits a realURL asking for a tinyURL, you check to see if that realURL is already there.
If so, you give them the tinyURL that already exists for it.
If not, you use some random method to generate tinyURLs until you get one that's not already in your database, insert (realURL, tinyURL) in the database, and give them the new tinyURL.
When a tinyURL request comes in to your website, you generate an http-redirect request to the realURL.
Actually generating random strings is pretty easy -- you could just have an array of "printable" characters like
, and use
to generate "tiny" strings until you get one that's not in your table already...