- /*
- Com_create_guid () is a feature supported by the PHP5 version and can be defined on its own for unsupported versions
- */
- function guid () {
- if (function_exists (' Com_create_guid ')) {
- return Com_create_guid ();
- }else{
- Mt_srand (Double) microtime () *10000)//optional for PHP 4.2.0 and up.
- Echo (Mt_rand ());
- $charid = Strtoupper (MD5 (Uniqid (rand (), true));
- $hyphen = Chr (45);//"-"
- $uuid = Chr (123)//"{"
- . substr ($charid, 0, 8). $hyphen
- . substr ($charid, 8, 4). $hyphen
- . substr ($charid, 4). $hyphen
- . substr ($charid, 4). $hyphen
- . substr ($charid, 20,12)
- . chr (125);//"}"
- return $uuid;
- }
- }
Copy Code2, MD5 and GUID will output 32 character hexadecimal number, the difference is that the GUID is randomly generated, MD5 need to be generated according to the input data. Example:
- $str = "Hello";
- echo MD5 ($STR);
- ?>
Copy CodeOutput: 8b1a9953c4611296a827abf8c47804d7 Advantages: Can be based on the input of the seed data to control the output value, if the seed data is not repeated, through the MD5 can protect the data, resulting in a great confusion; disadvantage: 32-bit characters are too long ; the need to provide non-duplicated seed data; usage: High concurrency, with seconds as the seed data, there will still be repetition.
- /*
- * Combined with the time () function, the number of seconds from 1970 years to the current period as the seed count
- */
- $str =time ();
- echo MD5 ($STR);
- ?>
Copy Code3, Uniqid (): Returns a 13 or 23-bit string for our purposes, uniqid () is like an improved version of MD5 (), especially if we can use the differential identifier as a string prefix to reduce the chance of duplicate naming occurrences. For extreme situations such as non-high concurrency, it is recommended to use this function to meet general requirements. Definition: the Uniqid () function generates a unique ID based on the current time in microseconds; Usage: uniqid (prefix,more_entropy); Description: prefix can add a prefix to the output string, as shown below, more_ When the entropy parameter is true, a 23-bit string is output.
- Var_dump (Uniqid ());
- Var_dump (Uniqid ("a"));
- ?>
Copy CodeThe output is: string ("51734aa562254" string "a51734aa562257" advantage: 13-bit string length, is an acceptable file name length, you can add a prefix, the result contains data confusion, to avoid the reverse of the original data ; disadvantage: Similar to MD5, high concurrency, in seconds as the seed data, there will still be repeated phenomenon. Third, the upgrade version of Scenario 1, Fast_uuid: Return 17 digits somewhat like uniqid () of the incomplete custom version, the function of the "seed number start time" concept is very enlightening. Time () and uniqid () are used by default in 1970, with a length of 10 bits (1366512439) and a "seed count start time" to narrow this value, because what we actually need is just a value that can be automatically increased. Since the start time customization, in addition to reducing the length, but also can play a role of confusion.
- /*
- * Parameter Suffix_len Specifies how many bits of random number are attached to the generated ID value, the default value is 3
- * @param int Suffix_len
- * @return String
- */
- function Fast_uuid ($suffix _len=3) {
- //! Calculate the start time of the seed number
- $being _timestamp = strtotime (' 2013-3-21 ');
- $time = Explode (' ', Microtime ());
- $id = ($time [1]-$being _timestamp). sprintf ('%06u ', substr ($time [0], 2, 6));
- if ($suffix _len > 0)
- {
- $id. = substr (sprintf ('%010u ', Mt_rand ()), 0, $suffix _len);
- }
- return $id;
- }
Copy CodeOutput: 29832412631099013 2, time () + random number in the above example, the use of random numbers has occurred in order to resolve multiple requests occurring in one second. provides two functions:
- function Random ($length) {
- $hash = ";
- $chars = ' abcdefghijklmnopqrstuvwxyz0123456789abcdefghijklmnopqrstuvwxyz ';
- $max = strlen ($chars)-1;
- Php_version < ' 4.2.0 ' && Mt_srand ((double) microtime () * 1000000);
- for ($i = 0; $i < $length; $i + +) {
- $hash. = $chars [Mt_rand (0, $max)];
- }
- return $hash;
- }
- function Random2 ($length, $numeric = 0) {
- Php_version < ' 4.2.0 '? Mt_srand (Double) microtime () * 1000000): Mt_srand ();
- $seed = Base_convert (MD5 (Print_r ($_server, 1). Microtime ()), $numeric? 10:35);
- $seed = $numeric? (Str_replace (' 0 ', ', $seed). ' 012340567890 '): ($seed. ' ZZ '. Strtoupper ($seed));
- $hash = ";
- $max = strlen ($seed)-1;
- for ($i = 0; $i < $length; $i + +) {
- $hash. = $seed [Mt_rand (0, $max)];
- }
- return $hash;
- }
- ?>
Copy CodeFour, the final plan idea: userid+ second + random number. where "userid+ seconds" 10 binary to 64, reducing the number of bits. Description: 1, userid:64 binary maximum "ZZZZ" is converted to decimal equals "16777215", "ZZZ" is converted to decimal maximum equals "262143", 2, second: Set its own time starting point. $less =time ()-strtotime (' 2012-4-21′) converted to 64 binary "1SpRe", 5-bit $less=time ()-strtotime (' 2013-3-21′) converted to 64 "_jhy", 4-bit 3, Random number: Generates 3-bit random numbers using random (3). Final Result: 4-bit userid+4 bit seconds + 3-bit random number = 11-bit string. Although the results look similar to the uniqid (), the strength has improved. 1 2 Next last page |