Complete example of generating a unique RequestID class for the PHP implementation

  • 2021-10-16 01:20:24
  • OfStack

In this paper, an example is given to describe the generation only 1RequestID class implemented by PHP. Share it for your reference, as follows:

This paper introduces PHP generating only 1RequestID class, using session_create_id () and uniqid () methods to ensure uniqueness, providing complete code and demonstration, which is convenient for everyone to learn and use.

The current system design generally uses distributed systems, and a request may call several micro-service processes, and finally return the results. When there is a problem with the request, it is difficult for us to track which microservice has a problem.

When each request accesses the server, We can add a only one identity (RequestID) to this access, and write the key data of this request process into the log at the beginning, process and end of the request (such as the parameters during the access, the methods, micro-services, the data returned at the end, etc.), which can be used as a reference when there is a problem in the access, so as to facilitate tracking the problem.

For example, a request needs to go through several microservices before returning output

Request- > A- > B- > C-A- > Output

If there is no output or an error in the access process, we can find the logs corresponding to A, B and C according to RequestID, and check which service has problems.

The code is as follows:

RequestID.class.php


<?php
/**
 * PHP Generate only 1RequestID Class 
 * Date: 2018-04-10
 * Author: fdipzone
 * Version: 1.0
 *
 * Description:
 * PHP Implement generation only 1RequestID Class, using the session_create_id() And uniqid() Method implementation, ensuring that only 1 Sex. 
 *
 * Func:
 * public generate  Generate only 1 Request id
 * private format  Format request id
 */
class RequestID{ // class start
 /**
  *  Generate only 1 Request id
  * @return String
  */
 public static function generate(){
  //  Use session_create_id() Method to create a prefix 
  $prefix = session_create_id(date('YmdHis'));
  //  Use uniqid() Method to create only 1id
  $request_id = strtoupper(md5(uniqid($prefix, true)));
  //  Format request id
  return self::format($request_id);
 }
 /**
  *  Format request id
  * @param String $request_id  Request id
  * @param Array $format   Format 
  * @return String
  */
 private static function format($request_id, $format='8,4,4,4,12'){
  $tmp = array();
  $offset = 0;
  $cut = explode(',', $format);
  //  Format according to settings 
  if($cut){
   foreach($cut as $v){
    $tmp[] = substr($request_id, $offset, $v);
    $offset += $v;
   }
  }
  //  Add the remainder 
  if($offset<strlen($request_id)){
   $tmp[] = substr($request_id, $offset);
  }
  return implode('-', $tmp);
 }
} // class end
?>

demo:


<?php
require 'RequestID.class.php';
//  Generate 10 Request id
for($i=0; $i<10; $i++){
 echo RequestID::generate().PHP_EOL;
}
?>

Output:

16532925-4502-CDAD-23A2-463FC7B5803A
500B77AD-CD24-0DDA-9E6E-2FDF2DD7CA94
813143D0-958F-9F56-E04F-679598594452
E5EE1B0B-E0D6-3E60-D831-462C5A262FCE
79E714B5-A37F-4B5E-4EDE-83E18391EBF9
E1C440AB-FC2C-AC74-E79A-016FD59D9651
AE483861-1040-BE8D-E523-D7638D0F0D35
BBD7A03A-36C9-24B7-C453-FB1DDD6E201E
BF62C3E6-9C5F-22CB-668D-381863B35268
E97E1F44-F048-962A-5BF7-1113727551B1

Attention session_create_id Method requires php version 7.1 or above to be used.

For session_create_id method, please refer to official website description:
http://php.net/manual/zh/function.session-create-id.php

Source code download address: Click here to download this site.

PS: Here are two related online tools for your reference:

Online random number/string generation tool:
http://tools.ofstack.com/aideddesign/suijishu

Online random character/random password generation tool:
http://tools.ofstack.com/aideddesign/rnd_password

Online random number generation tools:
http://tools.ofstack.com/aideddesign/rnd_num

Online random number/random password batch generation tool:
http://tools.ofstack.com/aideddesign/rnd_pwd_tool

For more readers interested in PHP related contents, please check the special topics of this site: "Summary of PHP Mathematical Operation Skills", "Summary of php String (string) Usage", "PHP Data Structure and Algorithm Tutorial", "Summary of php Programming Algorithm", "Encyclopedia of PHP Array (Array) Operation Skills" and "Summary of php Common Database Operation Skills"

I hope this article is helpful to everyone's PHP programming.


Related articles: