<?php
$abc = array('a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o','p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z', 'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O','P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z');

for ($i=0; $i < 6; $i++)
    {   
        $pass = array_rand($abc);
        echo $abc[$pass];
    }

?>

I need to send email with generated pass but i must generated pass get to some variable but i dont now how.

有帮助吗?

解决方案

I just use this: $newpass = substr(md5(uniqid()),0,8);

It's not particularly secure, but it does the job and users are required to change their passwords as soon as they log in with a reset password. The point is it's more efficient than loops and character accesses.

其他提示

You can use the mcrypt extention's iv function to get random data from the system. Because it is a bit-stream you need to encode it in some way, so that it is not garbage on the screen. Base64 encoding should suffice. Because base64 encoded strings are approximately 33% longer you need to cut the string down to the length you want.

Here is a simple example:

function password($length = 10) {
    $random = mcrypt_create_iv($length, MCRYPT_DEV_URANDOM);
    $base64 = base64_encode($random);
    return substr($base64, 0, $length);
}
var_dump(password(10));

User this snippet of code that I wrote a few days back.

function genText($length) {
    $out_str = '';
    $p_chars = "AGHACEFHJKRSTVXY124579aghacefhjkrstvxy"; // can be any set of characters
    while (strlen($out_str) < $length) {
        $out_str.= $p_chars{rand() % (strlen($p_chars))};
    }
    return $out_str;
}

$your_variable = genText(8);
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top