In our daily projects, we often encounter an environment where php is used to generate a unique digital id. I checked a lot of information on the Internet, but there is still a possibility of repetition, so today I will share my personal income from my recent research on this issue. Do you need to use rand to generate a random number and then go to the database to check whether the number exists? It seems a little time-consuming. Is there any other way?
Of course not. there are actually two solutions.
1. if you only use php instead of a database, the timestamp + random number is the best method without repetition;
2. if you need to use a database, you also need to associate this id with some other data. Then, an AUTO_INCREMENT (auto-increment) attribute is assigned to the table id in the MySQL database. each time a data entry is inserted, the id is automatically added to 1, and then mysql_insert_id () or LAST_INSERT_ID () is used () returns the auto-increment id.
Of course, there is already a ready-made solution to this problem. using the php uuid extension can perfectly solve this problem. this extension can generate a unique full digital signature ..
If you don't use composer please refer to https://github.com/lootils/uuid,
If your project is built based on composer, please refer to https://github.com/ramsey/uuid
I will not proceed with the specific source code. the friends can use it directly after getting it.
Sample code for generating a unique identifier in PHP:
<? // Generate a unique identifier // sha1 () function, "security hash algorithm (SHA1)" function create_unique () {$ data = $ _ SERVER ['http _ USER_AGENT ']. $ _ SERVER ['remote _ ADDR ']. time (). rand (); return sha1 ($ data); // return md5 (time (). $ data); // return $ data;}?>
Function description and example for generating unique identifiers in PHP
< ? $newhash = create_unique(); echo $newhash; ?>
Here is another example.
/** Semaphore ). * This is a packaging class used to solve different implementation methods of the "Semaphore" on different platforms. * Currently, this class is symbolic. it is actually a dry run on Windows platforms (and does not actually implement mutual exclusion ). */Class SemWrapper {private $ hasSemSupport; private $ sem; const SEM_KEY = 1; public function _ construct () {$ this-> hasSemSupport = function_exists ('sem _ get'); if ($ this-> hasSemSupport) {$ this-> sem = sem_get (self :: SEM_KEY) ;}} public function acquire () {if ($ this-> hasSemSupport) {return sem_acquire ($ this-> sem);} return true;} public function release () {if ($ this-> hasSemSupport) {Return sem_release ($ this-> sem) ;}return true ;}}/** sequence number generator. */Class SeqGenerator {const SHM_KEY = 1;/*** initialize the sequence number generator. * This method is valid only when it is called for the first time after the server is started. * @ Param int $ start generates the starting value of the sequence number. * @ Return boolean true indicates success. */Static public function init ($ start = 1) {// mutex is implemented through semaphores to avoid access conflicts to shared memory $ sw = new SemWrapper; if (! $ Sw-> acquire () {return false;} // open the shared memory $ shm_id = shmop_open (self: SHM_KEY, 'n', 0644, 4 ); if (empty ($ shm_id) {// because the 'n' mode is used, if the shared memory cannot be enabled, you can think that the shared memory has been created, no need to initialize $ sw-> release (); return true;} // write the initial value $ size = shmop_write ($ shm_id, pack ('L ', $ start), 0); if ($ size! = 4) {shmop_close ($ shm_id); $ sw-> release (); return false;} // Close the shared memory and release the semaphores shmop_close ($ shm_id ); $ sw-> release (); return true;}/*** generates the next sequence number. * @ Return int the sequence number */static public function next () {// mutex through semaphores to avoid access conflicts to shared memory $ sw = new SemWrapper; if (! $ Sw-> acquire () {return 0;} // open the shared memory $ shm_id = shmop_open (self: SHM_KEY, 'W', 0, 0 ); if (empty ($ shm_id) {$ sw-> release (); return 0;} // read the sequence number $ data = shmop_read ($ shm_id, 0, 4); if (empty ($ data) {$ sw-> release (); return 0 ;}$ arr = unpack ('L', $ data ); $ seq = $ arr [1]; // write the next sequence number to the shared memory $ size = shmop_write ($ shm_id, pack ('L', $ seq + 1), 0 ); if ($ size! = 4) {$ sw-> release (); return 0 ;}// close the shared memory and release the semaphores shmop_close ($ shm_id); $ sw-> release (); return $ seq ;}$ a = SeqGenerator: init (time (); var_dump ($ a); for ($ I = 0; $ I <10; $ I ++) {$ seq = SeqGenerator: next (); var_dump ($ seq );}
Now, let's go here today. I hope it will be helpful for you to learn PHP.