I've done something similar to this on http://filepublicator.com. My solution was that each post gets its own unique incremental id (just like you normally would give them: 1, 2, 3, etc..), and also a copy of this id converted to base 62.
Then I use this base 62 copy as the url. Heres another thread on SO about this: converting a number base 10 to base 62 (a-zA-Z0-9)
I went with this method of first generating a real id and then converting it to base 62 because then I know I have a unique id without testing it against my already existing posts and it can easily be translated to my real id on the fly.
Theoretically someone could easily figure out my method of creating urls and try to loop through all my pages, but if you are looking for making posts hidden and hard to find short urls without password protection isen't the way to go.
This is the method written by Eineki in that thread:
function toBase($num, $b=62) {
$base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$r = $num % $b ;
$res = $base[$r];
$q = floor($num/$b);
while ($q) {
$r = $q % $b;
$q =floor($q/$b);
$res = $base[$r].$res;
}
return $res;
}
function to10( $num, $b=62) {
$base='0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ';
$limit = strlen($num);
$res=strpos($base,$num[0]);
for($i=1;$i<$limit;$i++) {
$res = $b * $res + strpos($base,$num[$i]);
}
return $res;
}
The test
for ($i = 0; $i<1000000; $i++) {
$x = toBase($i);
$y = to10($x);
if ($i-$y)
echo "\n$i -> $x -> $y";
}