Creating a random id or a totally unique ID

By Twocans - November 5, 2015

Hello,

How would I best create a unique ID with the membership signup,  is there something I can call and place it on my page to show a unique id aka sha like

a7sdf7dfgtb7sdfsd798s7df9sdf

I have tried calling this on the signup page but lol it didnt work.

<?php echo htmlencode(@$_REQUEST['randomSaveId']); ?>

regards

kenny

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 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