| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950 |
- <?php
- namespace app\http;
- class RequestID
- {
- /**
- * 生成唯一请求id
- * @return string
- */
- public static function generate()
- {
- // 使用uniqid()方法创建唯一id
- $request_id = strtoupper(md5(uniqid($_SERVER['HTTP_USER_AGENT'],true)));
- // 格式化请求id
- return self::format($request_id);
- }
- /**
- * 格式化请求id
- *
- * @param string $request_id 请求id
- * @param string $format 格式
- *
- * @return string
- */
- private static function format($request_id, $format='16,4,4'){
- $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);
- }
- }
|