generating random page id with no duplicates

Discussion in 'PHP' started by Mr.only, Mar 6, 2011.

  1. #1
    i need function to generating random page id with no duplicates in db

    something like this

    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;
    }
    PHP:
    and i don't want this check

    $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...
    
            }  
    PHP:
    some one told me that i can use time function like this

    <?php
    $length1=11;
    function random_text($start,$length1)
    {
        return  substr(md5(time().rand()),$start,$length1);
    }
    
    $code1=random_text(10,11);
    echo $code1;
    echo"<br />"; 
    ?>
    PHP:
    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 page id and i want it completely like youtube id
    ex:http://www.youtube.com/watch?v=AxlJV89mFHE

    any help .
     
    Mr.only, Mar 6, 2011 IP
  2. plog

    plog Peon

    Messages:
    298
    Likes Received:
    11
    Best Answers:
    1
    Trophy Points:
    0
    #2
    I would make your id a number, specifically an autonumber. Then, when you want to display it you could run it through a hash function (i.e. md5). That way it ensures its unique and it displays seemingly randomly. Here's some examples:

    1 - c4ca4238a0b923820dcc509a6f75849b
    2 - c81e728d9d4c2f636f067f89cc14862c
    3 - eccbc87e4b5ce2fe28308fd9f2a7baf3
     
    plog, Mar 6, 2011 IP
  3. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #3
    <?php
    $str_alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $str_num=1234567890;
    $shuffle_alpha=str_shuffle($str_alpha);
    $shuffle_num=str_shuffle($str_num);
    $alpha=str_split($shuffle_alpha,7);
    $num=str_split($shuffle_num,3);
    $rand_alpha=mt_rand(0,6);
    $rand_num=mt_rand(0,2);
    $rand_alpha_output=$alpha[$rand_alpha];
    $rand_num_output=$num[$rand_num];
    $join_output=$rand_alpha_output.$rand_num_output;
    if(file_exists('printed_id.txt'))
    {
            $getID=file_get_contents('printed_id.txt');
            $printed_id=explode(PHP_EOL,$getID);    
            while($final_output=str_shuffle($join_output))
            {       
                    if(!in_array($final_output,$printed_id))
                    {
                            echo $final_output;
                            $final_output=str_shuffle($join_output);//valid id for further usage and processing
                            break;
                    }
            }
            $fileOPN=fopen('printed_id.txt','a+');
            fwrite($fileOPN,$final_output);
            fwrite($fileOPN,"\r\n");        
    }
    else
    {
            $final_output=str_shuffle($join_output);
            $fileOPN=fopen('printed_id.txt','a+');
            fwrite($fileOPN,$final_output);
            fwrite($fileOPN,"\r\n");
            echo $final_output;//valid id for further usage and processing
    }
    
    ?>
    PHP:
    this exactly does it all. If u r happy with it, leave a +ve reputation to my profile
     
    spaceman12, Mar 7, 2011 IP
  4. Mr.only

    Mr.only Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #4
    thanxx spaceman12
    but
    an error
    Fatal error: Call to undefined function: str_split() in c:\appserv\www\test.php on line 6
    in this line
    $alpha=str_split($shuffle_alpha,7);

    can you fix it for me please
    and are you sure this code never duplicate in db
     
    Mr.only, Mar 7, 2011 IP
  5. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #5
    
    <?php
    $str_alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $str_num=1234567890;
    $shuffle_alpha=str_shuffle($str_alpha);
    $shuffle_num=str_shuffle($str_num);
    $alpha=str_split($shuffle_alpha,7);
    $num=str_split($shuffle_num,3);
    $rand_alpha=mt_rand(0,6);
    $rand_num=mt_rand(0,2);
    $rand_alpha_output=$alpha[$rand_alpha];
    $rand_num_output=$num[$rand_num];
    $join_output=$rand_alpha_output.$rand_num_output;
    if(file_exists('printed_id.txt'))
    {
    	$getID=file_get_contents('printed_id.txt');
    	$printed_id=explode(PHP_EOL,$getID);	
    	while($final_output=str_shuffle($join_output))
    	{	
    		if(!in_array($final_output,$printed_id))
    		{
    			echo $final_output;
    			$final_output=str_shuffle($join_output);//valid id for further usage and processing
    			break;
    		}
    	}
    	$fileOPN=fopen('printed_id.txt','a+');
    	fwrite($fileOPN,$final_output);
    	fwrite($fileOPN,"\r\n");	
    }
    else
    {
    	$final_output=str_shuffle($join_output);
    	$fileOPN=fopen('printed_id.txt','a+');
    	fwrite($fileOPN,$final_output);
    	fwrite($fileOPN,"\r\n");
    	echo $final_output;//valid id for further usage and processing
    }
    
    ?>
    
    PHP:
    I'm sure you tried some alteration within the code that i wrote for you or otherwise there is zero error. I can execute it easily on any where
     
    spaceman12, Mar 7, 2011 IP
  6. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #6
    
    <?php
    $str_alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $str_num=1234567890;
    $shuffle_alpha=str_shuffle($str_alpha);
    $shuffle_num=str_shuffle($str_num);
    $alpha=str_split($shuffle_alpha,7);
    $num=str_split($shuffle_num,3);
    $rand_alpha=mt_rand(0,6);
    $rand_num=mt_rand(0,2);
    $rand_alpha_output=$alpha[$rand_alpha];
    $rand_num_output=$num[$rand_num];
    $join_output=$rand_alpha_output.$rand_num_output;
    if(file_exists('printed_id.txt'))
    {
    	$getID=file_get_contents('printed_id.txt');
    	$printed_id=explode(PHP_EOL,$getID);	
    	while($final_output=str_shuffle($join_output))
    	{	
    		if(!in_array($final_output,$printed_id))
    		{
    			echo $final_output;
    			$final_output=str_shuffle($join_output);//valid id for further usage and processing
    			break;
    		}
    	}
    	$fileOPN=fopen('printed_id.txt','a+');
    	fwrite($fileOPN,$final_output);
    	fwrite($fileOPN,"\r\n");	
    }
    else
    {
    	$final_output=str_shuffle($join_output);
    	$fileOPN=fopen('printed_id.txt','a+');
    	fwrite($fileOPN,$final_output);
    	fwrite($fileOPN,"\r\n");
    	echo $final_output;//valid id for further usage and processing
    }
    
    ?>
    
    PHP:
    I'm sure you tried some alteration within the code that i wrote for you or otherwise there is zero error. I can execute it easily on any where



    EDIT: My connection so poor that it came out this way as double post when I never intend to make it appear this way. Sorry!!

    EDIT:

    There is no chance of duplicating even it goes on generating the ID non-stop lasting for the whole year.......................................the while loop is implemented within the code so as to prevent double entry of the same ID into your db... 100% guarenteed !



    EDIT: RECTIFICATION MADE------THIS 100% error free and I revised it again. USE THIS CODE FOR UR PROJECT IMPLEMENTATION:
    
    <?php
    $str_alpha="abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";
    $str_num=1234567890;
    $shuffle_alpha=str_shuffle($str_alpha);
    $shuffle_num=str_shuffle($str_num);
    $alpha=str_split($shuffle_alpha,7);
    $num=str_split($shuffle_num,3);
    $rand_alpha=mt_rand(0,6);
    $rand_num=mt_rand(0,2);
    $rand_alpha_output=$alpha[$rand_alpha];
    $rand_num_output=$num[$rand_num];
    $join_output=$rand_alpha_output.$rand_num_output;
    if(file_exists('printed_id.txt'))
    {
    	$getID=file_get_contents('printed_id.txt');
    	$printed_id=explode(PHP_EOL,$getID);	
    	while($final_output=str_shuffle($join_output))
    	{	
    		if(!in_array($final_output,$printed_id))
    		{
    			$final_output=str_shuffle($join_output);//valid id for further usage and processing
    			break;
    		}
    	}
    	echo $final_output;
    	$fileOPN=fopen('printed_id.txt','a+');
    	fwrite($fileOPN,$final_output);
    	fwrite($fileOPN,"\r\n");
    exit();	
    }
    else
    {
    	$final_output=str_shuffle($join_output);
    	$fileOPN=fopen('printed_id.txt','a+');
    	fwrite($fileOPN,$final_output);
    	fwrite($fileOPN,"\r\n");
    	echo $final_output;//valid id for further usage and processing
    }
    
    ?>
    
    PHP:
     
    Last edited: Mar 7, 2011
    spaceman12, Mar 7, 2011 IP
  7. Mr.only

    Mr.only Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Fatal error: Call to undefined function: str_split() in c:\appserv\www\test.php on line 6

    I'm sorry. the same error in the same line .. did you think that error in my localhost ?
     
    Mr.only, Mar 7, 2011 IP
  8. Mr.only

    Mr.only Peon

    Messages:
    37
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #8
    thaanxxxxxxxxxxxxxx
    it's work's will but not on the localhost ..
    why doesn't work in local host ??
    can i add ( - and _) ?

    Done !
     
    Last edited: Mar 7, 2011
    Mr.only, Mar 7, 2011 IP
  9. spaceman12

    spaceman12 Active Member

    Messages:
    60
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    63
    #9
    maybe the error in ur localhost is because u used an older version of php?...lol though not quite sure
     
    spaceman12, Mar 7, 2011 IP
  10. crazyryan

    crazyryan Well-Known Member

    Messages:
    3,087
    Likes Received:
    165
    Best Answers:
    0
    Trophy Points:
    175
    #10
    
    <?php
    $unique = md5(uniqid());
    ?>
    
    Code (markup):
     
    crazyryan, Mar 7, 2011 IP