Home php教程 php手册 PHP生成不重复标识符程序代码

PHP生成不重复标识符程序代码

May 25, 2016 pm 04:42 PM
substr

生成唯一不重复的标识我们主要是根据当前的一个时间time然后再转换在md5值,这样几乎是可以保证标签的唯一性了,下面整理了一些关于PHP生成不重复标识符程序代码,希望能各位有帮助。

PHP倒是自带了生成唯一id的函数:uniqid() ,它是基于当前时间微秒数的,用法如下:

echo uniqid(); //13位的字符串

echo uniqid("php_"); //当然你可以加上前缀

echo uniqid("php_", TRUE); //如果第二个参数more_entropy为true则生成23位

字符串

但是它生成的标识有可能不是唯一的,所以很多人会:

<?php
    //这是第一种简单的方法,当然用sha1()函数也可以。
    echo md5(uniqid());
    //第二种,利用时间戳的方法
    echo md5(time() . mt_rand(1,1000000));
?>
Copy after login

例子。

<?php
//生成唯一标识符
//sha1()函数, "安全散列算法(SHA1)"
function create_unique() {
    $data = $_SERVER[&#39;HTTP_USER_AGENT&#39;] . $_SERVER[&#39;REMOTE_ADDR&#39;]
     .time() . rand();
    return sha1($data);
    //return md5(time().$data);
    //return $data;
}
?>
Copy after login

例子如下:

<?php
$newhash = create_unique();
echo $newhash; 
?>
Copy after login

我看到很多人使用 md5() 函数,即使它并不完全意味着这个目的:

// generate unique string

echo md5(time() . mt_rand(1,1000000));

There is actually a PHP function named uniqid() that is meant to be used for this.

// generate unique string

echo uniqid();

/* prints

4bd67c947233e

*/

// generate another unique string

echo uniqid();

/* prints

4bd67c9472340

*/

你可能会注意到,尽管字符串是唯一的,前几个字符却是类似的,这是因为生成的字符串与服务器时间相关。

但实际上也存在友好的一方面,由于每个新生成的 ID 会按字母顺序排列,这样排序就变得很简单。

为了减少重复的概率,你可以传递一个前缀,或第二个参数来增加:


// with prefix

echo uniqid('foo_');

/* prints

foo_4bd67d6cd8b8f

*/

// with more entropy

echo uniqid('',true);

/* prints

4bd67d6cd8b926.12135106

*/

// both

echo uniqid('bar_',true);

/* prints

bar_4bd67da367b650.43684647

*/

这个函数将产生比 md5() 更短的字符串,节省一些空间。

php生成全球唯一标识符(GUID)的方法

GUID在空间上和时间上具有唯一性,保证同一时间不同地方产生的数字不同。

世界上的任何两台计算机都不会生成重复的 GUID 值。

需要GUID的时候,可以完全由算法自动生成,不需要一个权威机构来管理。

GUID的长度固定,并且相对而言较短小,非常适合于排序、标识和存储。

<?php
//php生成GUID
function getGuid() {
    $charid = strtoupper(md5(uniqid(mt_rand(), true))); 
    $hyphen = chr(45);// "-" 
    $uuid = substr($charid, 0, 8).$hyphen 
    .substr($charid, 8, 4).$hyphen 
    .substr($charid,12, 4).$hyphen 
    .substr($charid,16, 4).$hyphen 
    .substr($charid,20,12);
    return $uuid; 
}
?>
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

AI Hentai Generator

AI Hentai Generator

Generate AI Hentai for free.

Hot Article

R.E.P.O. Energy Crystals Explained and What They Do (Yellow Crystal)
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. Best Graphic Settings
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
R.E.P.O. How to Fix Audio if You Can't Hear Anyone
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
WWE 2K25: How To Unlock Everything In MyRise
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

PHP returns the ASCII value of the first character of the string PHP returns the ASCII value of the first character of the string Mar 21, 2024 am 11:01 AM

This article will explain in detail the ASCII value of the first character of the string returned by PHP. The editor thinks it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. PHP returns the ASCII value of the first character of a string Introduction In PHP, getting the ASCII value of the first character of a string is a common operation that involves basic knowledge of string processing and character encoding. ASCII values ​​are used to represent the numeric value of characters in computer systems and are critical for character comparison, data transmission and storage. The process of getting the ASCII value of the first character of a string involves the following steps: Get String: Determine the string for which you want to get the ASCII value. It can be a variable or a string constant

PHP returns the string from the start position to the end position of a string in another string PHP returns the string from the start position to the end position of a string in another string Mar 21, 2024 am 10:31 AM

This article will explain in detail how PHP returns the string from the start position to the end position of a string in another string. The editor thinks it is quite practical, so I share it with you as a reference. I hope you will finish reading this article. You can gain something from this article. Use the substr() function in PHP to extract substrings from a string. The substr() function can extract characters within a specified range from a string. The syntax is as follows: substr(string,start,length) where: string: the original string from which the substring is to be extracted. start: The index of the starting position of the substring (starting from 0). length (optional): The length of the substring. If not specified, then

Understand the substr() function in PHP to intercept strings Understand the substr() function in PHP to intercept strings Nov 18, 2023 am 11:27 AM

Understand the substr() function in PHP for intercepting strings. In the PHP language, the substr() function is a very useful string processing function, which can be used to intercept string fragments at a specified position and length. The substr() function accepts three parameters: the string to be intercepted, the starting position of the interception, and the length of the interception. Below we will introduce the use of the substr() function in detail and give specific code examples. Basic usage of substr() function substr() function

Use the PHP function 'substr' to get the substring of a string Use the PHP function 'substr' to get the substring of a string Jul 24, 2023 pm 10:13 PM

Use the PHP function "substr" to obtain the substring of a string. In PHP programming, you often encounter situations where you need to obtain part of the content of a string. At this time, we can use PHP's built-in function "substr" to achieve this. This article explains how to use the "substr" function to get a substring of a string and provides some code examples. 1. Basic usage of the substr function The substr function is used to obtain a substring of a specified length from a string. Its basic syntax is as follows: substr(

PHP mb_substr function invalid solution PHP mb_substr function invalid solution Mar 22, 2024 am 09:00 AM

Solutions for invalid PHPmb_substr function When developing PHP applications, the mb_substr function is often used to intercept strings. However, sometimes you may encounter situations where the mb_substr function is invalid, mainly due to character encoding issues in different environments. In order to solve this problem, we need to effectively handle the mb_substr function. A common solution is to ensure that the mb_substr function can

PHP convert first letter of string to lowercase PHP convert first letter of string to lowercase Mar 21, 2024 pm 02:11 PM

This article will explain in detail how PHP converts the first letter of a string to lowercase. I think it is very practical, so I share it with you as a reference. I hope you can gain something after reading this article. Converting the first letter of a PHP string to lowercase Introduction In PHP, converting the first letter of a string to lowercase is a common operation. This can be achieved by using the built-in function lcfirst() or the string operator strtolower(). This guide will dive into both approaches, providing example code and best practices. Method 1: Use the lcfirst() function The lcfirst() function is specifically designed to convert the first letter of a string to lowercase while leaving the rest of the characters unchanged. Its syntax is as follows: st

The substr_replace() function in PHP language replaces a substring of a specified length. The substr_replace() function in PHP language replaces a substring of a specified length. Jun 27, 2023 pm 03:15 PM

The substr_replace() function in the PHP language is a very practical string processing function, which can be used to replace a substring of a specified length. The syntax of the substr_replace() function is as follows: substr_replace($string,$replacement,$start[,$length]); where $string represents the original string to be replaced, and $replacement represents the replaced string.

Use the PHP function 'substr' to return the substring of a string Use the PHP function 'substr' to return the substring of a string Jul 24, 2023 pm 05:55 PM

Use the PHP function "substr" to return a substring of a string. In PHP, we often need to perform some operations on strings, such as intercepting part of a string. At this time, we can use PHP's built-in function "substr" to achieve this function. The substr function returns a substring of a string based on the specified starting position and length. Here is a simple example that demonstrates how to use the substr function to intercept part of a string: &lt;?php$str=&quot

See all articles