php如何生成唯一值session_create_id()
,下面编程教程网小编给大家详细介绍一下具体代码!
实现代码如下:
<?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>';
}
以上是编程学习网小编为您介绍的“php如何生成唯一值session_create_id”的全面内容,想了解更多关于 php入门 内容,请继续关注编程基础学习网。
沃梦达教程
本文标题为:php如何生成唯一值session_create_id
猜你喜欢
- PHP实现微信扫码登录功能的两种方式总结 2023-07-03
- php数组函数序列之array_slice() – 在数组中根据条件取出一段值,并返回 2023-12-30
- 关于简单的php源代码泄露漏洞的发掘 2023-12-12
- php多进程应用场景实例详解 2023-01-31
- PHP执行linux命令6个函数代码实例 2023-05-03
- PHP Class self 与 static 异同与使用详解 2022-09-02
- PHP设计模式的策略,适配器和观察者模式详解 2023-06-26
- PHP中PCRE正则解析代码详解 2023-01-08
- PHP实现统计一个数字在排序数组中出现次数的方法 2022-10-05
- laravel在中间件内生成参数并且传递到控制器中的2种姿势 2023-03-02