Search code examples
phpmysqlloopsrandomauto-generate

Auto generate token does not functioning properly in php


I am now creating a project. In this I am stuck. I want that an 8 digit pin number is generate. If this pin is already in the database then another pin number is generated. After counting 100 times if in all 100 times the generated pin number is in the database then a 9 digit pin number is generated. My Code is given below :

$t=$this->generate_string(8);
$th= Booking::Where('token',$t)->first();
$count = 0 ;
if(isset($th)) {
    if($count >= 100){
        $t   = $this->generate_string(9);
    }else{
        $t   = $this->generate_string(8);
    }
    $count++;
}
$booking->token = $t;

And generate_string function are

private function generate_string($length)
{
    $character = '0123456789';
    $character .= 'abcdefghijklmnopqrstuvwxyz';
    $quantity_character = strlen($character);
    $quantity_character--;
    $Hash = NULL;
    for ($x = 1; $x <= $length; $x++) {
        $position = rand(0, $quantity_character);
        $Hash .= substr($character, $position, 1);
    }
    return $Hash;
}

My code is not functioning properly. Please help me


Solution

  • Your code will not increment the counter because you did not do it.

    I created an example based on your code, i used recursion to get the token.

    Generate Token Function:

    private function generate_string($length)
    {
        $character = '0123456789';
        $character .= 'abcdefghijklmnopqrstuvwxyz';
        $quantity_character = strlen($character);
        $quantity_character--;
        $Hash = NULL;
        for ($x = 1; $x <= $length; $x++) {
            $position = rand(0, $quantity_character);
            $Hash .= substr($character, $position, 1);
        }
        return $Hash;
    }
    

    Check token function:

    private function check_token($t){
        return Booking::Where('token',$t)->first();
    }
    

    Get token function:

    private function get_token($length, $count){
        $t=$this->generate_string($length);
    
        $r = check_token($t);
        if(!isset($r)) // base case 1
            return $t;
        if($count <= 0) // base case 2
            return $this->generate_string(9);
    
        return $this->get_token($length, --$count); // decrements the $count and do recursion.
    }
    

    Usage: $token = $this->get_token(9, 5); //token length = 9, max tries = 5

    Get token function uses recursion to generate the token, how the recursion will stop? we have to base cases,

    1. When !isset(check_token($t)) mean that there is not possible token duplicate.
    2. When $count <= 0 mean that we reach the max possible tries.

    If no one of base cases matched, the function call it self again with decremented $count.

    Check and update it to fit your needs.

    UPDATE:

    To get token if the token matches n times, update the get_token function as following:

    private function get_token($length, $count){
        $t=generate_string($length);
    
        $r = check_token($t);
        if(isset($r)){ // token matches, then decrements $count 
            --$count;
        }else{
            return $t; // token did not matched, then return the token
        }
        if($count <= 0) // if we reaches the max tries, return token of length '$length + 1'
            return $this->generate_string($length + 1); 
    
        return $this->get_token($length, $count);
    }