Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to generate 20 characters alphanumeric unique id

Tags:

php

hash

I want to generate 20 characters of hash code in php and insert in to table and the hash code should be unique.

And later the hash code can be used to access records in table.

<?php
   echo uniqid();
   //generate 20 charcter alphanumeric id here
?>

How can i do this?

like image 341
CJAY Avatar asked Feb 09 '23 15:02

CJAY


1 Answers

Try this, it will always generate a different, unique string dependent on microtime and random number.

<?php
     // Here microtime will be unique everytime
     echo substr(md5(microtime()*rand(0,9999)),0,20);
 ?>
like image 52
Pawan Kumar Sharma Avatar answered Feb 12 '23 10:02

Pawan Kumar Sharma