Creating a random id or a totally unique ID

By Daryl - November 10, 2015

Hi Kenny,

You can use uniqid("yourprefix_") to generate a unique ID. 
And you might want to check the generated ID against your records, if it already exists, generate a new one.

It may not be the best way for your purpose, but it's the easiest.
Do you need it to be human-readable or extremely hard to guess or it doesn't matter as long as it is unique?

Cheers,

Daryl Maximo
PHP Programmer - interactivetools.com

By Twocans - November 12, 2015 - edited: November 12, 2015

Daryl Thank You,
I just wanted something totally unique. 

<?php echo md5(uniqid(rand(), true)); ?>

the above works but i am not sure how unique it would be, I was just wondering did the cms have something I could call which would be long and unique

regards

kenny

By Daryl - November 12, 2015

Hi Kenny,

I usually use websiteMembership plugin's wsm_generatePassword() function for generating random strings, which is basically:

substr(md5(uniqid(mt_rand(), true)), 15)

I think it will generate an id unique enough for usual unique id use. And to double check if it's unique, I checked it against the records, generate a new one if a match is found.
You can also increase the uniqueness of the string by adding timestamp:

$uniqueId = time() . substr(md5(uniqid(mt_rand(), true)), 15);

Just curious, where are you going to use the unique ID?

Daryl Maximo
PHP Programmer - interactivetools.com

By Twocans - December 21, 2015

i was trying to use it for a download link.

cheers

kenny