generating random code no duplicates like youtube pages id
Posted: Sun Mar 06, 2011 4:06 pm
i need function to generating random page id with no duplicates in db
something like this
and i don't want this check
some one told me that i can use time function like this
it works and not needs to make checking in db but the problem is its give me numbers more than letters
and actuality i need use it as pages id and i want it completely like youtube id without duplicate
ex:http://www.youtube.com/watch?v=AxlJV89mFHE
any help .
something like this
Code: Select all
function generate_id($length2 = 11)
{
$range = 'abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890-_';
$max = strlen($range) - 1;
// Generates random id of length $length.
$id = '';
for ($count = 0; $count < $length2; $count++)
{
$id .= $range[rand(0, $max)];
}
return $id;
}Code: Select all
$check = mysqli_query($mysqli, "SELECT ".$table.".token FROM ".$table." WHERE ".$table.".token = '".$random."'");
if(mysqli_num_rows($check) > 0){
//ERROR: DUPLICATE TOKEN, CREATE A NEW TOKEN NOW...
}Code: Select all
<?php
$length1=11;
function random_text($start,$length1)
{
return substr(md5(time().rand()),$start,$length1);
}
$code1=random_text(10,11);
echo $code1;
echo"<br />";
?>and actuality i need use it as pages id and i want it completely like youtube id without duplicate
ex:http://www.youtube.com/watch?v=AxlJV89mFHE
any help .