PHP generates a secure random password program

Source: Internet
Author: User
PHP generates a random password, easy and fast, can randomly generate a secure and reliable password, I hope this article will be helpful to everyone.

PHP generates a secure random password program instance code as follows:

<?php

Header ("Content-type:text/html;charset=utf-8");

function Getrandpass ($length = 6) {

$password = ";

Add the characters you want to the following string, which is the number 0-9 and the 26 English letters by default

$chars = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ";

$char _len = strlen ($chars);

for ($i =0; $i < $length; $i + +) {

$loop = Mt_rand (0, ($char _len-1));

Take this string as an array, randomly take out a character, and loop it into the number of bits you need.

$password. = $chars [$loop];

}

return $password;

}

Echo Getrandpass (12); Randomly generate a 12-digit password

Open Source Code phpfensi.com

?>

Example 2, with the first one a little like

1. Preset a string $chars, including a–z,a–z,0–9, and some special characters

2. Randomly take a character in a $chars string

3, repeat the second step n times, can get the length of the password of n

The instance code is as follows:

function Generate_password ($length = 8) {

Password character set, you can add any character you need

$chars = ' abcdefghijklmnopqrstuvwxyzabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&* ()-_ []{}<>~ ' +=,.;:/?| ';

$password = ";

for ($i = 0; $i < $length; $i + +)

{

Here are two ways to get a character

The first is to use SUBSTR to intercept any one character in the $chars;

The second type is to take any element $chars the character array

$password. = substr ($chars, Mt_rand (0, strlen ($chars)-1), 1);

$password. = $chars [Mt_rand (0, strlen ($chars)-1)];

}

return $password;

}

  • 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.