Regarding the issue of generating a unique digital ID, is it necessary to generate a random number using Rand and then go to the database query for this number? Feel this kind of words a little time-consuming, is there any other way?
Of course not, in fact there are two ways to solve.
1. If you only use PHP instead of database, the timestamp + random number is the best method, and do not repeat;
2. If you need to use a database, you also need to associate some other data with this ID. Then give the ID of the table in the MySQL database a auto_increment attribute, each time you insert a piece of data, the ID is automatically +1, and then use MYSQL_INSERT_ID () or last_insert_id () to return the self-added ID.
Of course, there is already a workaround for this problem, and using the PHP UUID extension is a perfect solution to this problem, and this extension produces a unique, fully digital signature.
If you do not use composer please refer to Https://github.com/lootils/uuid,
If your project is built based on composer, please refer to the Https://github.com/ramsey/uuid
The specific source code I do not carry, the small partners themselves take down can be used directly
PHP generates a unique identifier code example:
<?
Generates a unique identifier
//SHA1 () function, the Secure Hash Algorithm (SHA1) functions
Create_unique () {
$data = $_server[' http_user_agent ']. $_ server[' REMOTE_ADDR ']
. Time () rand ();
Return SHA1 ($DATA);
Return MD5 (Time (). $data);
return $data;
>
PHP generates a unique identifier function description and example
<?
$newhash = Create_unique ();
echo $newhash;
? >
To share one more
* * Signal Volume (semaphore).
* This is a wrapper class that addresses the different implementations of the "semaphore" under different platforms.
* At present this class is only symbolic, in the Windows platform is actually empty run (and does not really implement mutexes).
* * 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);
The public function acquire () {if ($this->hassemsupport) {return Sem_acquire ($this->sem);
return true;
The 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.
* The first call is valid only after the server is started, and calling this method after it has no practical effect.
* @param int $start produces a sequence number starting value.
* @return Boolean returns true to indicate success.
/static Public function init ($start = 1) {//Mutex is achieved by semaphore, avoiding access to shared memory $SW = new Semwrapper;
if (! $SW->acquire ()) { return false;
//Open Shared memory $shm _id = Shmop_open (Self::shm_key, ' n ', 0644, 4);
if (Empty ($shm _id)) {//due to use ' n ' mode, if the shared memory cannot be opened, it can be assumed that the shared memory has been created without having to initialize $SW->release () again;
return true;
///write initial value in shared memory $size = Shmop_write ($shm _id, pack (' L ', $start), 0);
if ($size!= 4) {shmop_close ($shm _id);
$SW->release ();
return false;
///Turn off shared memory, release semaphore Shmop_close ($shm _id);
$SW->release ();
return true;
/** * produces the next sequence number.
* The sequence number produced by the @return int */static public function next () {//mutual exclusion through semaphores to avoid access to shared memory $SW = new Semwrapper;
if (! $SW->acquire ()) {return 0;
//Open Shared memory $shm _id = Shmop_open (Self::shm_key, ' W ', 0, 0);
if (Empty ($shm _id)) {$SW->release ();
return 0;
//Read sequence number from shared memory $data = Shmop_read ($shm _id, 0, 4);
if (empty ($data)) {$SW->release ();
return 0; } $arr = Unpack(' L ', $data);
$seq = $arr [1];
Writes 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;
///Turn off shared memory, release semaphore Shmop_close ($shm _id);
$SW->release ();
return $seq;
}} $a = Seqgenerator::init (time ());
Var_dump ($a);
For ($i =0 $i < $i + +) {$seq = Seqgenerator::next ();
Var_dump ($SEQ); }
OK, let's get to the bar today, and I want to learn PHP to help you.