cakephp: uuid component
来源:互联网 发布:软件程序开发公司 编辑:程序博客网 时间:2024/06/05 23:40
<?php
/**
* A simple component which generates UUIDs.
*
* Component created by Daniel Hofstetter (http://cakebaker.42dh.com)
*/
class UuidComponent extends Object {
/**
* Generates a Universally Unique IDentifier, version 4.
*
* RFC 4122 (http://www.ietf.org/rfc/rfc4122.txt) defines a special type of Globally
* Unique IDentifiers (GUID), as well as several methods for producing them. One
* such method, described in section 4.4, is based on truly random or pseudo-random
* number generators, and is therefore implementable in a language like PHP.
*
* We choose to produce pseudo-random numbers with the Mersenne Twister, and to always
* limit single generated numbers to 16 bits (ie. the decimal value 65535). That is
* because, even on 32-bit systems, PHP's RAND_MAX will often be the maximum *signed*
* value, with only the equivalent of 31 significant bits. Producing two 16-bit random
* numbers to make up a 32-bit one is less efficient, but guarantees that all 32 bits
* are random.
*
* The algorithm for version 4 UUIDs (ie. those based on random number generators)
* states that all 128 bits separated into the various fields (32 bits, 16 bits, 16 bits,
* 8 bits and 8 bits, 48 bits) should be random, except : (a) the version number should
* be the last 4 bits in the 3rd field, and (b) bits 6 and 7 of the 4th field should
* be 01. We try to conform to that definition as efficiently as possible, generating
* smaller values where possible, and minimizing the number of base conversions.
*
* @copyright Copyright (c) CFD Labs, 2006. This function may be used freely for
* any purpose ; it is distributed without any form of warranty whatsoever.
* @author David Holmes <dholmes@cfdsoftware.net>
*
* @return string A UUID, made up of 32 hex digits and 4 hyphens.
*/
function generate() {
// The field names refer to RFC 4122 section 4.1.2
return sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
mt_rand(0, 65535), // 16 bits for "time_mid"
mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
// 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
// (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
// 8 bits for "clk_seq_low"
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
);
}
}
?>
/**
* A simple component which generates UUIDs.
*
* Component created by Daniel Hofstetter (http://cakebaker.42dh.com)
*/
class UuidComponent extends Object {
/**
* Generates a Universally Unique IDentifier, version 4.
*
* RFC 4122 (http://www.ietf.org/rfc/rfc4122.txt) defines a special type of Globally
* Unique IDentifiers (GUID), as well as several methods for producing them. One
* such method, described in section 4.4, is based on truly random or pseudo-random
* number generators, and is therefore implementable in a language like PHP.
*
* We choose to produce pseudo-random numbers with the Mersenne Twister, and to always
* limit single generated numbers to 16 bits (ie. the decimal value 65535). That is
* because, even on 32-bit systems, PHP's RAND_MAX will often be the maximum *signed*
* value, with only the equivalent of 31 significant bits. Producing two 16-bit random
* numbers to make up a 32-bit one is less efficient, but guarantees that all 32 bits
* are random.
*
* The algorithm for version 4 UUIDs (ie. those based on random number generators)
* states that all 128 bits separated into the various fields (32 bits, 16 bits, 16 bits,
* 8 bits and 8 bits, 48 bits) should be random, except : (a) the version number should
* be the last 4 bits in the 3rd field, and (b) bits 6 and 7 of the 4th field should
* be 01. We try to conform to that definition as efficiently as possible, generating
* smaller values where possible, and minimizing the number of base conversions.
*
* @copyright Copyright (c) CFD Labs, 2006. This function may be used freely for
* any purpose ; it is distributed without any form of warranty whatsoever.
* @author David Holmes <dholmes@cfdsoftware.net>
*
* @return string A UUID, made up of 32 hex digits and 4 hyphens.
*/
function generate() {
// The field names refer to RFC 4122 section 4.1.2
return sprintf('%04x%04x-%04x-%03x4-%04x-%04x%04x%04x',
mt_rand(0, 65535), mt_rand(0, 65535), // 32 bits for "time_low"
mt_rand(0, 65535), // 16 bits for "time_mid"
mt_rand(0, 4095), // 12 bits before the 0100 of (version) 4 for "time_hi_and_version"
bindec(substr_replace(sprintf('%016b', mt_rand(0, 65535)), '01', 6, 2)),
// 8 bits, the last two of which (positions 6 and 7) are 01, for "clk_seq_hi_res"
// (hence, the 2nd hex digit after the 3rd hyphen can only be 1, 5, 9 or d)
// 8 bits for "clk_seq_low"
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535) // 48 bits for "node"
);
}
}
?>
要使用Uuid组件,吧uuid.php文件放在/app/controllers/components ,在控制器
$this->Uuid->generate();
- cakephp: uuid component
- customize cookie component in cakephp
- cakephp
- CakePHP
- cakephp
- cakephp
- CakePHP
- CakePHP
- CakePHP
- CakePHP
- UUID
- UUID
- UUID
- uuid
- UUID
- uuid
- UUID
- uuid
- turbo C库函数
- 哈希表问题
- shell 编程笔记:shell 命令(二)连载
- 在十自路口
- sql server 2000数据库服务器,常出现数据库引擎停止与启动,这是为什么?
- cakephp: uuid component
- Java的常用包之八
- 开张
- 正则表达式收藏与整理
- 博客换不了VC、厕所站不来投资:) 商业计划书是一定要写的
- 项目管理书籍
- 习惯的改变和习惯的建立
- 两天的业务培训下来,那真叫累
- db2资料收集