Example of an algorithm for generating globally unique guid using php

Source: Internet
Author: User
Example of an algorithm for generating globally unique guid using php
This article describes how to use php to generate a guid with a globally unique identifier. For more information, see.

1. create the php class library file-guid. php

     name = $coumputer_name;    $address->ip   = $ip;    return $address;  }  function toString() {    return strtolower($this->name.'/'.$this->ip);  }}class Random {  function nextLong() {    $tmp = rand(0,1)?'-':'';    return $tmp.rand(1000, 9999).rand(1000, 9999).rand(1000, 9999).rand(100, 999).rand(100, 999);  }}class Guid{  var $valueBeforeMD5;  var $valueAfterMD5;  function Guid($computer_name, $ip){    $this->getGuid($computer_name, $ip);  } //by bbs.it-home.org function getGuid($coumputer_name, $ip){   $address = NetAddress::getHost($coumputer_name, $ip);   $this->valueBeforeMD5 = $address->toString().':'.System::currentTimeMillis().':'.Random::nextLong();   $this->valueAfterMD5 = md5($this->valueBeforeMD5);  }    function newGuid() {   $Guid = new Guid();   return $Guid;  }   function toString() {   $raw = strtoupper($this->valueAfterMD5);   return substr($raw,0,8).'-'.substr($raw,8,4).'-'.substr($raw,12,4).'-'.substr($raw,16,4).'-'.substr($raw,20);  }}?>

2. call an example to generate a unique Gid:

     ToString (); // output result: 3238D32E-807C-B1C4-01C4-FD1346D32110?>

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.