|
Génération de mot de passe en PHP |
|
Written by Red
|
|
Tuesday, 27 January 2009 11:43 |
Cette fonction permet de générer un mot de passe en PHP. Elle prend 2 paramètres :
- La longueur du mot de passe
- La force du mot de passe
Voici un exemple avec les source pour vos faire une idée.
<?php
function generatePassword($length=9, $strength=0) {
$vowels = 'aeuy';
$consonants = 'bdghjmnpqrstvz';
if ($strength & 1) {
$consonants .= 'BDGHJLMNPQRSTVWXZ';
}
if ($strength & 2) {
$vowels .= "AEUY";
}
if ($strength & 4) {
$consonants .= '23456789';
}
if ($strength & 8) {
$consonants .= '@#$%';
}
$password = '';
$alt = time() % 2;
for ($i = 0; $i < $length; $i++) {
if ($alt == 1) {
$password .= $consonants[(rand() % strlen($consonants))];
$alt = 0;
} else {
$password .= $vowels[(rand() % strlen($vowels))];
$alt = 1;
}
}
return $password;
}
?>
Ce code n'est pas de moi, je l'ai récupéré ici : http://www.webtoolkit.info/php-random-password-generator.html
|