PHP Conference Japan 2024

com_create_guid

(PHP 5, PHP 7, PHP 8)

com_create_guid產生全域唯一識別碼 (GUID)

說明

com_create_guid(): 字串|false

產生全域唯一識別碼 (GUID)。

GUID 的產生方式與 DCE UUID 相同,但 Microsoft 的慣例是將 GUID 括在大括號中。

參數

此函式沒有參數。

回傳值

成功時回傳字串形式的 GUID,失敗時回傳 false

參見

  • PECL uuid 擴充中的 uuid_create()

新增筆記

使用者貢獻的筆記 5 則筆記

Dave Pearson (dave at pds-uk dot com)
8 年前
這是我最終版本的 GUIDv4 函式(基於這裡其他人的成果),它應該可以在所有平台上運作,並在其他版本不支援的情況下優雅地回退到較不安全的版本...

<?php
/**
* Returns a GUIDv4 string
*
* Uses the best cryptographically secure method
* for all supported pltforms with fallback to an older,
* less secure version.
*
* @param bool $trim
* @return string
*/
function GUIDv4 ($trim = true)
{
// Windows
if (function_exists('com_create_guid') === true) {
if (
$trim === true)
return
trim(com_create_guid(), '{}');
else
return
com_create_guid();
}

// OSX/Linux
if (function_exists('openssl_random_pseudo_bytes') === true) {
$data = openssl_random_pseudo_bytes(16);
$data[6] = chr(ord($data[6]) & 0x0f | 0x40); // set version to 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // set bits 6-7 to 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}

// Fallback (PHP 4.2+)
mt_srand((double)microtime() * 10000);
$charid = strtolower(md5(uniqid(rand(), true)));
$hyphen = chr(45); // "-"
$lbrace = $trim ? "" : chr(123); // "{"
$rbrace = $trim ? "" : chr(125); // "}"
$guidv4 = $lbrace.
substr($charid, 0, 8).$hyphen.
substr($charid, 8, 4).$hyphen.
substr($charid, 12, 4).$hyphen.
substr($charid, 16, 4).$hyphen.
substr($charid, 20, 12).
$rbrace;
return
$guidv4;
}
?>
Alix Axel
14 年前
phunction PHP 框架 (http://sourceforge.net/projects/phunction/) 使用以下函式來產生有效的版本 4 UUID

<?php

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));
}

?>

由 sprintf() 和 mt_rand() 呼叫產生的輸出與 com_create_guid() 的結果相同。
pavel.volyntsev(at)gmail
9 年前
使用更安全的加密演算法來產生偽隨機位元組,並將其格式化為 GUID v4 字串

function guidv4()
{
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); // 將版本設為 0100
$data[8] = chr(ord($data[8]) & 0x3f | 0x80); // 將第 6-7 位元設為 10
return vsprintf('%s%s-%s-%s-%s-%s%s%s', str_split(bin2hex($data), 4));
}
indrora
9 年前
如果您要產生隨機 UUID,至少要符合以下規範:

* 第三組的第一個位元組必須是 4
* 第四組的第一個位元組可以是 (8 9 a b) 其中之一

另請參閱:UUID 的維基百科頁面:https://en.wikipedia.org/wiki/Universally_unique_identifier#Version_4_.28random.29
internmail (at the google mail).com
4 年前
function create_guid() { // 建立 GUID(全域唯一識別碼)
$guid = '';
$namespace = rand(11111, 99999);
$uid = uniqid('', true);
$data = $namespace;
$data .= $_SERVER['REQUEST_TIME'];
$data .= $_SERVER['HTTP_USER_AGENT'];
$data .= $_SERVER['REMOTE_ADDR'];
$data .= $_SERVER['REMOTE_PORT'];
$hash = strtoupper(hash('ripemd128', $uid . $guid . md5($data)));
$guid = substr($hash, 0, 8) . '-' .
substr($hash, 8, 4) . '-' .
substr($hash, 12, 4) . '-' .
substr($hash, 16, 4) . '-' .
substr($hash, 20, 12);
return $guid;
}
To Top