php如何生成唯一值session_create_id()
,下面web建站小编给大家详细介绍一下具体代码!
实现代码如下:
复制代码<?php
class RequestID{
public static function generateV7(){
// 使用session_create_id()方法创建前缀
$prefix = session_create_id(date('YmdHis'));
// 使用uniqid()方法创建唯一id
$request_id = strtoupper(md5(uniqid($prefix, true)));
// 格式化请求id
return self::format($request_id);
}
public static function generate(){
// 创建前缀
$prefix = self::create_guid(date('YmdHis'));
// 使用uniqid()方法创建唯一id
$request_id = strtoupper(md5(uniqid($prefix, true)));
// 格式化请求id
return self::format($request_id);
}
public static function create_guid($namespace = '') {
static $guid = '';
$uid = uniqid("", true);
$data = $namespace;
$data .= $_SERVER['REQUEST_TIME'];
$data .= $_SERVER['HTTP_USER_AGENT'];
$data .= isset($_SERVER['LOCAL_ADDR'])?$_SERVER['LOCAL_ADDR']:$_SERVER['SERVER_ADDR'];
$data .= isset($_SERVER['LOCAL_PORT'])?$_SERVER['LOCAL_PORT']:$_SERVER['SERVER_PORT'];
$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;
}
// 格式化请求id
private static function format($request_id, $format='8,4,4,4,12'){
$tmp = array();
$offset = 0;
$cut = explode(',', $format);
// 根据设定格式化
if($cut){
foreach($cut as $v){
$tmp[] = substr($request_id, $offset, $v);
$offset += $v;
}
}
// 加入剩余部分
if($offset<strlen($request_id)){
$tmp[] = substr($request_id, $offset);
}
return implode('-', $tmp);
}
}
// 生成10个请求id
for($i=0; $i<10; $i++){
echo RequestID::generate().PHP_EOL.'<br>';
}
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
- 50
- 51
- 52
- 53
- 54
- 55
- 56
- 57
- 58
- 59
- 60
- 61
- 62
- 63
- 64
- 65
- 66
- 67
- 68
- 69
- 70
- 71
- 72
- 73
- 74
- 75
标签: 生成唯一值
上面是“php如何生成唯一值session_create_id”的全面内容,想了解更多关于 php入门 内容,请继续关注web建站教程。
当前网址:https://m.ipkd.cn/webs_3376.html
声明:本站提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请发送到邮箱:admin@ipkd.cn,我们会在看到邮件的第一时间内为您处理!