Generate guid in PHP (tested)

Source: Internet
Author: User

Guid. Class. php file

PHP code
  1. <? PHP
  2. Class System
  3. {
  4. Function currenttimemillis ()
  5. {
  6. List ($ USEC, $ Sec) = explode ("", microtime ());
  7. Return $ sec. substr ($ USEC, 2, 3 );
  8. }
  9. }
  10. Class netaddress
  11. {
  12. VaR $ name = 'localhost ';
  13. VaR $ IP = '1970. 0.0.1 ';
  14. Function getlocalhost () // static
  15. {
  16. $ Address = new netaddress ();
  17. $ Address-> name = $ _ env ["computername"];
  18. $ Address-> IP = $ _ server ["server_addr"];
  19. Return $ address;
  20. }
  21. Function tostring ()
  22. {
  23. Return strtolower ($ this-> name. '/'. $ this-> ip );
  24. }
  25. }
  26. Class random
  27. {
  28. Function nextlong ()
  29. {
  30. $ TMP = rand (0, 1 )? '-':'';
  31. Return $ TMP. Rand (1000,999 9). Rand (1000,999 9). Rand (1000,999 9). Rand (100,999). Rand (100,999 );
  32. }
  33. }
  34. // Three paragraphs
  35. // Segment is microsecond segment is address segment is random number
  36. Class guid
  37. {
  38. VaR $ valuebeforemd5;
  39. VaR $ valueaftermd5;
  40. Function GUID ()
  41. {
  42. $ This-> getguid ();
  43. }
  44. //
  45. Function getguid ()
  46. {
  47. $ Address = netaddress: getlocalhost ();
  48. $ This-> valuebeforemd5 = $ address-> tostring (). ':'. System: currenttimemillis (). ':'. Random: nextlong ();
  49. $ This-> valueaftermd5 = MD5 ($ this-> valuebeforemd5 );
  50. }
  51. Function newguid ()
  52. {
  53. $ Guid = new GUID ();
  54. Return $ guid;
  55. }
  56. Function tostring ()
  57. {
  58. $ Raw = strtoupper ($ this-> valueaftermd5 );
  59. Return substr ($ raw, 0, 8 ). '-'. substr ($ raw, 8, 4 ). '-'. substr ($ raw, 12, 4 ). '-'. substr ($ raw, 16, 4 ). '-'. substr ($ raw, 20 );
  60. }
  61. }
  62. ?>

Use

PHP code
  1. <? PHP
  2. Require_once ("guid. Class. php ");
  3. $ Guid = new GUID ();
  4. Print $ guid-> tostring ();
  5. ?>

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.