Skip to content

Instantly share code, notes, and snippets.

@olivsinz
Created May 23, 2020 12:23
Show Gist options
  • Save olivsinz/2a2e39d122d2f6a296c4948b9466602c to your computer and use it in GitHub Desktop.
Save olivsinz/2a2e39d122d2f6a296c4948b9466602c to your computer and use it in GitHub Desktop.
PHP Script to Create a unique alphanumeric string like Voucher Code, Referral Code, Coupon Code, OTP or any kind of Promotional Code
<?php
/**
*
*/
trait Uniquable
{
// this function can create a unique alphanumeric string. To create a
// unique string contains letter and number (alphanumeric) with the character limit $limit. These kinds of
// unique string can be used as Voucher Code, Referral Code, Coupon Code, OTP or any kind of Promotional Code.
//
// In the above function, I’m using the default PHP functions.
// base_convert – Convert a number between arbitrary bases.
// sha1 – Calculate the sha1 hash of a string.
// uniqid – Generate a unique ID.
// mt_rand – Generate a random value via the Mersenne Twister Random Number Generator.
function unique_code($limit)
{
return substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, $limit);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment