Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

php unique identifier for clients

I have a unique identifier for client i generate everytime a client request a service support.

once the id is generate, the id is inserted in the database

here my code:

function makeUnique() {
 $start_time = uniqid(microtime(1));
 $duration = sprintf('%0.24f', $start_time);
 return date('dmYHis').$duration;
}

echo makeUnique();

this output: 071120112032291320715949.928639888763427734375000

for some reason i get 071120112032291320715949 as the number. what am i doing wrong?

like image 371
David Peter Avatar asked Nov 08 '11 01:11

David Peter


2 Answers

you need to remove the "." (dot) in your function. you are probably using an INT which remove numbers after the dot.

return str_replace('.', '', date('dmYHis').$duration);

and make sure the field is big enough - like varchar(50)

my recommendation will be to simply hash the client id using md5

md5($clientid)

and you have the mysql field as a char(32)

like image 91
Book Of Zeus Avatar answered Oct 02 '22 05:10

Book Of Zeus


You've already got your solution in your code. The php function uniqid() provides... well, a unique ID. If you want to ensure it's really really really really unique (though it's not necessary), just append time() to the end, like so:

    return uniqid().time();

This would also return letters mixed in with numbers (higher entropy), like this response:

    4eb8895a76edc1320716634
like image 25
Jemaclus Avatar answered Oct 02 '22 06:10

Jemaclus