


Summary of methods for generating random passwords in PHP, summary of PHP generation_PHP tutorial
Summary of methods for generating random passwords in PHP, summary of PHP generation
Using PHP to develop applications, especially website programs, often requires generating random passwords, such as user registration to generate random passwords, users Resetting your password also requires generating a random password. A random password is a string of fixed length. Here I have collected several methods of generating random strings for your reference.
Method 1:
1. Generate a random integer from 33 – 126, such as 35,
2. Convert 35 into the corresponding ASCII character, such as 35 corresponding to #
3. Repeat the above steps 1 and 2 n times to connect into an n-digit password
This algorithm mainly uses two functions. The mt_rand (int $min, int $max) function is used to generate random integers, where $min – $max is the range of ASCII codes. Here, it is 33-126, which can be used as needed. Adjustment range, for example, 97 – 122 bits in the ASCII code table correspond to the English letters a – z. For details, please refer to the ASCII code table; chr (int $ascii) function is used to convert the corresponding integer $ascii into the corresponding character.
1 2 3 4 5 6 7 8 9 |
|
// Call this function and pass the length parameter $pw_length = 6
1 |
|
Method 2:
1. Preset a string $chars, including a – z, A – Z, 0 – 9, and some special characters
2. Randomly pick a character from the $chars string
3. Repeat the second step n times to get a password of length n
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Method 3:
1. Preset a character array $chars, including a – z, A – Z, 0 – 9, and some special characters
2. Randomly select $length elements from the array $chars through array_rand()
3. According to the obtained key name array $keys, take out the characters from the array $chars and concatenate the string. The disadvantage of this method is that the same characters will not be retrieved repeatedly.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
|
Method 4:
This method is a new method provided by a netizen after this article was reprinted by Blue Ideal. The algorithm is simple and the code is short. Just because of the return value of the md5() function, the generated password only includes letters and numbers. But it’s still a good method. Algorithmic thinking:
1. time() gets the current Unix timestamp
2. Encrypt the timestamp obtained in the first step with md5()
3. Intercept n digits from the encryption result of the second step to get the desired password
1 2 3 4 5 |
|
Time efficiency comparison
We use the following PHP code to calculate the running time of the above four random password generation functions to generate a 6-digit password, and then conduct a simple comparison of their time efficiency.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
|
Method 5:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
|
The final result is:
Method 1: 9.8943710327148E-5 seconds
Method 2: 9.6797943115234E-5 seconds
Method 3: 0.00017499923706055 seconds
Method 4: 3.4093856811523E-5 seconds
It can be seen that the execution time of method one and method two is similar. Method four has the shortest running time, while method three has a slightly longer running time.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



PHP 8.4 brings several new features, security improvements, and performance improvements with healthy amounts of feature deprecations and removals. This guide explains how to install PHP 8.4 or upgrade to PHP 8.4 on Ubuntu, Debian, or their derivati

To work with date and time in cakephp4, we are going to make use of the available FrozenTime class.

CakePHP is an open-source framework for PHP. It is intended to make developing, deploying and maintaining applications much easier. CakePHP is based on a MVC-like architecture that is both powerful and easy to grasp. Models, Views, and Controllers gu

To work on file upload we are going to use the form helper. Here, is an example for file upload.

Validator can be created by adding the following two lines in the controller.

Logging in CakePHP is a very easy task. You just have to use one function. You can log errors, exceptions, user activities, action taken by users, for any background process like cronjob. Logging data in CakePHP is easy. The log() function is provide

Visual Studio Code, also known as VS Code, is a free source code editor — or integrated development environment (IDE) — available for all major operating systems. With a large collection of extensions for many programming languages, VS Code can be c

CakePHP is an open source MVC framework. It makes developing, deploying and maintaining applications much easier. CakePHP has a number of libraries to reduce the overload of most common tasks.
