0
<?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.

3 Answers3

2

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.

Niet the Dark Absol
  • 320,036
  • 81
  • 464
  • 592
  • +1. `base64_encode(md5(uniqid()))` can give a tough password. – AKS Feb 07 '13 at 19:23
  • @AyeshK But a long one. Do you really want your users to have to type in a 45-character long password just to get back on their account? – Niet the Dark Absol Feb 07 '13 at 19:29
  • Sorry I meant to put your substr() there. `substr(base64_encode(md5(uniqid())),0,8);` – AKS Feb 07 '13 at 19:34
  • @AyeshK - What is the base64_encode for? An MD5 will already return an encoded value, so you gain nothing. – martinstoeckli Feb 07 '13 at 20:14
  • md5 returns only lowercase English letters and numbers. base64_encode will give you uppercase characters as well, making the password tough. – AKS Feb 08 '13 at 08:45
2

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));
Sverri M. Olsen
  • 13,055
  • 3
  • 36
  • 52
0

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);
Amir Bilal
  • 447
  • 3
  • 7