How to generate a new GUID?
I'm working on a web service which requires a new GUID()
passed as a reference to a method within the service.
I am not familiar with C#
or the GUID() object
, but require something similar for PHP
(so create a new object which from my understanding returns an empty/blank GUID
).
Any ideas?
You can try the following:
function GUID()
{
if (function_exists('com_create_guid') === true)
{
return trim(com_create_guid(), '{}');
}
return sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
}
Source - com_create_guid
As an alternative to the above options:
$guid = bin2hex(openssl_random_pseudo_bytes(16));
It gives a string like 412ab7489d8b332b17a2ae127058f4eb
<?php
function guid(){
if (function_exists('com_create_guid') === true)
return trim(com_create_guid(), '{}');
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40);
$data[8] = chr(ord($data[8]) & 0x3f | 0x80);
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
?>
GUID生成器
链接地址: http://www.djcxy.com/p/91436.html下一篇: 如何生成新的GUID?