Home php教程 php手册 php生成随机密码函数(四款)

php生成随机密码函数(四款)

May 25, 2016 pm 04:57 PM

下面提供了四款php生成随机密码函数哦,方法简单实用是一款用户自定的加密函数,这样如果不知道你的加密算法是很难破解的。

方法一

 代码如下 复制代码

function generate_password( $length = 8 ) {
    // 密码字符集,可任意添加你需要的字符
    $chars = 'abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*()-_ []{}~`+=,.;:/?|';

    $password = '';
    for ( $i = 0; $i     {
        // 这里提供两种字符获取方式
        // 第一种是使用 substr 截取$chars中的任意一位字符;
        // 第二种是取字符数组 $chars 的任意元素
        // $password .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        $password .= $chars[ mt_rand(0, strlen($chars) - 1) ];
    }

    return $password;
}

方法二

动生成6位数字、字母 混合密码

 代码如下 复制代码
$str = "0123456789abcdefghijklmnopqrstuvwxyz";   //   输出字符集 
$n = 6;   //   输出串长度 
$len = strlen($str)-1;
for($j=0 ; $jfor($i=0 ; $i    $s .=  $str[rand(0,$len)]; 
}
echo $s . "
";
$s = "";
}
?>
自动生成数字、字母、符号的密码
      $a = "12345678";
      $b = "abcdefghijklmnopqistuvwxyz";
      $s = substr(str_shuffle($a), 0, 2);
      $e = substr(str_shuffle($b), 0, 2);
      echo $s . substr(str_shuffle("!@#$%^&*"), 0, 2) . $e;
?>

方法三

 代码如下 复制代码

function create_password($pw_length = 8)
{
    $randpwd = '';
    for ($i = 0; $i     {
        $randpwd .= chr(mt_rand(33, 126));
    }
    return $randpwd;
}

// 调用该函数,传递长度参数$pw_length = 6
echo create_password(6);


方法三

 代码如下 复制代码

function getmicrotime()
{
    list($usec, $sec) = explode(" ",microtime());
    return ((float)$usec + (float)$sec);
}
 
// 记录开始时间
$time_start = getmicrotime();
   
// 这里放要执行的php代码,如:
// echo create_password(6);
 
// 记录结束时间
$time_end = getmicrotime();
$time = $time_end - $time_start;

 // 输出运行总时间
echo "执行时间 $time seconds";
?>



本文地址:

转载随意,但请附上文章地址:-)

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

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

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)