根据我的理解php生成随机密码就是我们把一些要生成的字符预置一个的字符串包括数字拼音之类的以及一些特殊字符,这样我们再随机取字符组成我们想要的随机密码了。
下面总结了一些实例各位朋友可参考。
例1
最简洁的生成方法
代码如下 | 复制代码 |
|
例2
1、在 33 – 126 中生成一个随机整数,如 35,
2、将 35 转换成对应的ASCII码字符,如 35 对应 #
3、重复以上 1、2 步骤 n 次,连接成 n 位的密码
代码如下 | 复制代码 |
function create_password($pw_length = 8) // 调用该函数,传递长度参数$pw_length = 6 |
实例
代码如下 | 复制代码 |
mt_srand((double) microtime() * 1000000); 密码生成器v4.0 by freemouse if (isset($_GET['password_length'])){ if(preg_match("/([0-9]{1,8})/", $_GET['password_length'])){ print("密码生成成功: " . gen_random_password($_GET['password_length']) . " n"); } else { print("密码长度不正确! n"); } } print 请为密码生成其指定生成密码的长度: end; ?> |
例4
1、预置一个的字符串 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、在 $chars 字符串中随机取一个字符
3、重复第二步 n 次,可得长度为 n 的密码
代码如下 | 复制代码 |
function generate_password( $length = 8 ) { $password = ''; return $password; |
上面经过测试性能都不如下面这个
1、预置一个的字符数组 $chars ,包括 a – z,A – Z,0 – 9,以及一些特殊字符
2、通过array_rand()从数组 $chars 中随机选出 $length 个元素
3、根据已获取的键名数组 $keys,从数组 $chars 取出字符拼接字符串。该方法的缺点是相同的字符不会重复取。
代码如下 | 复制代码 |
function make_password( $length = 8 ) // 在 $chars 中随机取 $length 个数组元素键名 $password = ''; return $password; |

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

Dreamweaver CS6
Visual web development tools

Atom editor mac version download
The most popular open source editor

Zend Studio 13.0.1
Powerful PHP integrated development environment

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

DVWA
Damn Vulnerable Web App (DVWA) is a PHP/MySQL web application that is very vulnerable. Its main goals are to be an aid for security professionals to test their skills and tools in a legal environment, to help web developers better understand the process of securing web applications, and to help teachers/students teach/learn in a classroom environment Web application security. The goal of DVWA is to practice some of the most common web vulnerabilities through a simple and straightforward interface, with varying degrees of difficulty. Please note that this software