Generate guid in PHP (tested)

Source: Internet
Author: User

 

Guid. Class. php file

PHP code
<? PHP
Class System
{
Function currenttimemillis ()
{
List ($ USEC, $ Sec) = explode ("", microtime ());
Return $ sec. substr ($ USEC, 2, 3 );
}
}
Class netaddress
{
VaR $ name = 'localhost ';
VaR $ IP = '1970. 0.0.1 ';
Function getlocalhost () // static
{
$ Address = new netaddress ();
$ Address-> name = $ _ env ["computername"];
$ Address-> IP = $ _ server ["server_addr"];
Return $ address;
}
Function tostring ()
{
Return strtolower ($ this-> name. '/'. $ this-> ip );
}
}
Class random
{
Function nextlong ()
{
$ TMP = rand (0, 1 )? '-':'';
Return $ TMP. Rand (1000,999 9). Rand (1000,999 9). Rand (1000,999 9). Rand (100,999). Rand (100,999 );

}
}
// Three paragraphs
// Segment is microsecond segment is address segment is random number
Class guid
{
VaR $ valuebeforemd5;
VaR $ valueaftermd5;
Function GUID ()
{
$ This-> getguid ();
}
//
Function getguid ()
{
$ Address = netaddress: getlocalhost ();
$ 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 );

}
}
?>

UsePHP code

<?php    require_once("guid.class.php");    $Guid = new Guid();    print $Guid->toString();    ?>    

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.