PHP algorithm -- generate a unique string

Source: Internet
Author: User
Tags 04x sha1

If you forget your password, click "forgot password", and then you will receive a link to change the password. There will always be a long and messy string in the connection. If a duplicate string is displayed, wouldn't someone else's password be reset?

Therefore, generating unique numbers is important to website security.

Currently, PHP can generate unique numbers using hash values, random numbers, time, and other methods. below is the unique number I tested. I personally recommend guid.

Method 1:

<?PHP$ Str=Uniqid(Mt_rand(), 1);Echo'Sha1 :',Sha1($ Str);Echo'<Br>';Echo'Md5 :',MD5($ Str);?>

Use the uniqid function and the mt_rand random function. Finally, use the hash value to obtain the final unique value.

However, during the test, it is found that the uniqueness of the random number is somewhat the same, and there are about 11 Identical digits (but there is no problem)

Method 2: guid

<? PHP  Echo Sprintf('% 04x % 04x-% 04x-% 04x-% 04x-% 04x % 04x % 04x ',Mt_rand(0, 65535 ),Mt_rand(0, 65535 ),Mt_rand(0, 65535 ),Mt_rand(16384,204 79 ),Mt_rand(51, 32768,491 ),Mt_rand(0, 65535 ),Mt_rand(0, 65535 ),Mt_rand(0, 65535)));  Echo "<Br>" ;  Echo   Sprintf ('% 04x % 04x-% 04x-% 04x-% 04x-% 04x % 04x % 04x ', Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (16384,204 79 ), Mt_rand (51, 32768,491 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535) ));  Echo "<Br>" ;  Echo  Sprintf ('% 04x % 04x-% 04x-% 04x-% 04x-% 04x % 04x % 04x ', Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (16384,204 79 ), Mt_rand (51, 32768,491 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535) ));  Echo "<Br>" ; Echo   Sprintf ('% 04x % 04x-% 04x-% 04x-% 04x-% 04x % 04x % 04x ', Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (16384,204 79 ), Mt_rand (51, 32768,491 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535 ), Mt_rand (0, 65535) )); ?>

introduction of UUID can be referred to: http://www.cnblogs.com/ghj1976/archive/2011/09/21/2184029.html

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.