The tinyurl.com page has an example of urls you can use. One of them is tinyurl.com/3 which redirects to a unicycle page. I’d always assumed that they encode the url, in the same way as a zip file is an encoded version of the real file, but I guess they just have a database which keeps track of the codes. So somewhere in that database, there’s a place that says id=3 link=”unicyclepage.com/blah.html”.
It used to be that they were sequential, so if you type in tinyurl.com/4 then tinyurl.com/5 you can see those pages in the order they were input to the system. It’s always a security risk to do this, since you’ll get people who like to make scripts to go through all the pages. And you get people who can predict what the next one will be and mess with it like this.
Yesterday, I needed a way to refer to pages in a database, but didn’t want to use the primary key id (which auto increments) to do it. So I made a script to generate tinyurl style random patterns.
// make a tinyurl.com style id for pages. this one only generates 4 (or more) character
// alphanumerics. there are about 1.6 million, should ben enough for most people.
// it's not fussy about making pottymouth urls, like sex1 or feck, but the chances
// are pretty slim... Oops, I ran it 5 times and got sexx! What're the odds?
// examples: v7or vfbt im0x b7tu 59jx 3a4k a7kw mvg9 supe 1rx9 4ww6 tgpn ezb7 z6h1// let's see some examples
for ($i=0; $i<14; $i++) {
echo make_tiny(13);
echo " ";
}function make_tiny ($how_long = 4) {
// make an array with the alphabet
$alphabetnum = array('a','b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm',
'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 1, 2, 3, 4, 5, 6, 7, 8, 9, 0);
$return = "";
for ($i=0; $i<$how_long; $i++) {
$char = rand (0, 35);
$return .= $alphabetnum[$char];
}
// could include some checking here, to see that you really have a unique id
// for example, look in a database and compare the ids that are there
// there's a chance that this will get recursive on your ass (if all ids are taken)
// it needs extra checks to avoid an infinite loop
if ($return == "v7or") { $return = make_tiny(); }
return $return;
}