PHP generates a unique digital ID method Rollup _php Tips

Source: Internet
Author: User
Tags generator learn php pack rand semaphore sha1 uuid

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.