PHP generates a unique numeric ID method rollup, PHP generation ID Rollup _php tutorial

Source: Internet
Author: User
Tags learn php sha1

PHP generated unique numeric id method summary, PHP generation ID Summary


Regarding the question of generating a unique numeric ID, is it necessary to use Rand to generate a random number and then go to the database query for this number? It's a bit of a time to feel like this, is there any other way?

Of course not, there are two ways to solve.

1. If you use PHP instead of the database, then the timestamp + random number is the best method, and does not repeat;

2. If you need to use a database, you also need to associate some additional data with the ID. That gives the ID of the table in the MySQL database a auto_increment (self-increment) attribute, ID automatically +1 each time a piece of data is inserted, and then returns the self-increment ID with mysql_insert_id () or last_insert_id ().

Of course, this problem already has a ready-made solution, the use of PHP uuid extension can be a perfect solution to this problem, this extension can generate a unique full 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 Https://github.com/ramsey/uuid

The specific source code I will not carry, the small partners to take down their own can be used directly

PHP generates a unique identifier code example:

<? Generate a unique identifier  //SHA1 () function, "Secure 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;  

PHP generates a unique identifier function description and examples

<?  $newhash = Create_unique ();  echo $newhash;  ? >

And then we'll share a

/* Signal Volume (Semaphore). * This is a wrapper class that addresses different implementations of "semaphores" under different platforms. * This class is only symbolic at the moment, and it is actually an empty run under the Windows platform (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);    }} 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; }}/* * Sequential number generator.  */class seqgenerator{Const SHM_KEY = 1;   /** * Initialize the sequence number generator.   * Only the first call after server startup is valid, and calling this method after this does not actually work.   * @param int $start The starting value of the sequence number.   * @return Boolean returns true to indicate success.    */static Public function init ($start = 1) {//mutual exclusion via semaphores, avoid access violation of 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 the use of ' n ' mode, if the shared memory cannot be opened, you can assume that the shared memory has been created without having to initialize the $SW->release () again;    return true;    }//write the 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;    }//Close shared memory, release semaphore Shmop_close ($shm _id);    $SW->release ();  return true;   }/** * produces the next sequential number.    * The order number generated by the @return int */static public function next () {//is mutually exclusive through semaphores to avoid access violation of 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;    }//reads the sequence number from the shared memory $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 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 here today, I hope we can help you learn PHP.

http://www.bkjia.com/PHPjc/1073130.html www.bkjia.com true http://www.bkjia.com/PHPjc/1073130.html techarticle PHP generates a unique numeric ID of the method summary, the PHP generation ID rollup about generating a unique digital ID question, is it necessary to use Rand to generate a random number, and then go to the database query whether there is this ...

  • 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.