Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to generate unique referral code in php

Tags:

php


I have created E-commerce website, In user system, I created referral system works like this -> When a visitor create an account then unique referral code for that customer will be generated. I fear that referral code should not be matched when I'll have a lot of users. So, I wanna create unique referral code. I am creating like this:

$referral_code = strtolower(substr($first_name,0,3)).$this->refer_code(3);

public function refer_code($limit){
    return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
}

Here, I am picking first 3 letters from user name and 3 random letters. It's generating referral code like this:

illqhx

But my boss said that It's very difficult to read and tell to other. So, he wants that referral code should be only numbers or 3 letters from name and 3 numbers should be generated automatically and it should be unique, and limit should be 5 or 6.
Please help me

like image 700
Zain Shabir Avatar asked Sep 17 '25 22:09

Zain Shabir


2 Answers

Try this

function random_strings($length_of_string) 
{ 
    $str_result = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ1234567890abcdefghijklmnopqrstuvwxyz'; 
    return substr(str_shuffle($str_result), 0, $length_of_string); 
} 

// This function will generate 
// Random string of length 10 
echo random_strings(10); 

?> 

I recommend adding something like the following to your User class. You can use createReferralCode from the outside to retrieve the code. It will always return the same value. You could also just return $this and use an accessor method to retrieve the value.

How you save or validate your new key I'm leaving up to you.

/**
 * Referral Code
 *
 * @var string
 */
protected $referralCode;

/**
 * Create a referral code and store it on the User.
 *
 * @return string
 */
public function createReferralCode() : string
{
    if (empty($this->referralCode)) {
        // attempt to create a referral code until the one you have is unique
        do {
            $referralCode = $this->generateReferralCode();
        } while (!$this->hasUniqueReferralCode($referralCode));

        $this->referralCode = $referralCode;
    }

    return $this->referralCode;
}

/**
 * Generate a referral code.
 *
 * @return string
 */
protected function generateReferralCode() : string
{
    // generate crypto secure byte string
    $bytes = random_bytes(8);

    // convert to alphanumeric (also with =, + and /) string
    $encoded = base64_encode($bytes);

    // remove the chars we don't want
    $stripped = str_replace(['=', '+', '/'], '', $encoded);

    // get the prefix from the user name
    $prefix = strtolower(substr($this->firstName, 0, 3));

    // format the final referral code
    return ($prefix . $stripped);
}

/**
 * Check if the referral code is unique.
 *
 * @param  string  $referralCode
 *
 * @return boolean
 */
protected function hasUniqueReferralCode(string $referralCode) : bool
{
    // check against database to enforce uniqueness
}
like image 25
Dissident Rage Avatar answered Sep 20 '25 12:09

Dissident Rage



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!