note 92711 modified in function.mt-rand by vrana

From: Date: Wed, 23 Sep 2009 09:47:41 +0000
Subject: note 92711 modified in function.mt-rand by vrana
References: 1  Groups: php.notes 
Request: Send a blank email to php-notes+get-160970@lists.php.net to get a copy of this message
Just another random password generator. Assembles passwords such as these: N_v85kA2_s 8k4jz94_H0 30824n6VcN useful for... whatever. ;) $underscores holds the maximum allowed number of underscores. Default is two. The function is programmed to not add an underscore at the beginning or end of the password, right after another underscore, or if the maximum # has already been used. $length holds the length you want the password to be. Default is 10 characters long. <?php function alphanumericPass() { // Do not modify anything below here $underscores = 2; // Maximum number of underscores allowed in password $length = 10; // Length of password $p =""; for ($i=0;$i<$length;$i++) { $c = mt_rand(1,7); switch ($c) { case ($c<=2): // Add a number $p .= mt_rand(0,9); break; case ($c<=4): // Add an uppercase letter $p .= chr(mt_rand(65,90)); break; case ($c<=6): // Add a lowercase letter $p .= chr(mt_rand(97,122)); break; case 7: $len = strlen($p); if ($underscores>0&&$len>0&&$len<($length-1)&&$p[$len-1]!="_") { $p .= "_"; $underscores--; } else { $i--; continue; } break; } } return $p; } ?> --was-- Just another random password generator. Assembles passwords such as these: N_v85kA2_s 8k4jz94_H0 30824n6VcN useful for... whatever. ;) $underscores holds the maximum allowed number of underscores. Default is two. The function is programmed to not add an underscore at the beginning or end of the password, right after another underscore, or if the maximum # has already been used. $length holds the length you want the password to be. Default is 10 characters long. <?php function alphanumericPass() { // Do not modify anything below here $underscores = 2; // Maximum number of underscores allowed in password $length = 10; // Length of password $p =""; for ($i=0;$i<$length;$i++) { $c = mt_rand(1,7); switch ($c) { case ($c<=2): // Add a number $p .= mt_rand(0,9); break; case ($c<=4): // Add an uppercase letter $p .= chr(mt_rand(65,90)); break; case ($c<=6): // Add a lowercase letter $p .= chr(mt_rand(97,122)); break; case 7: $len = strlen($p); if ($underscores>0&&$len>0&&$len<9&&$p[$len-1]!="_") { $p .= "_"; $underscores--; } else { $i--; continue; } break; } } return $p; } ?> http://php.net/manual/en/function.mt-rand.php

« previous php.notes (#160970) next »