Prepare for your Next Interview
|
Welcome to the Geeks Talk forums. You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community you will have access to post topics, communicate privately with other members (PM), respond to polls, upload content and access many other special features. Registration is fast, simple and absolutely free so please, join our community today! If you have any problems with the registration process or your account login, please contact contact us. |
This is a discussion on Generate random password Using PHP within the PHP forums, part of the Web Development category; Hi, I want to know how to generate random password in PHP. One of the ways of achieving this is to write a function for the same. I know that ...
|
|||||||
| PHP PHP - The most popular scripting language. Discuss PHP related questions here. Sample Scripts, Popular downloads, tools and utilites and more... |
![]() |
| LinkBack | Thread Tools | Display Modes |
|
|||
|
Generate random password Using PHP
Hi,
I want to know how to generate random password in PHP. One of the ways of achieving this is to write a function for the same. I know that there are many methods of doing this. Can someone provide some other methods of doing that? Regards, Fred |
| Sponsored Links |
|
|||
|
<?
function Random_Password($length) { srand(date("s")); $possible_charactors = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ"; $string = ""; while(strlen($string)<$length) { $string .= substr($possible_charactors, rand()%(strlen($possible_charactors))),1); } return($string); } echo Random_Password(8); ?> ------------------------------------------- <? function genpassword($length){ srand((double)microtime()*1000000); $vowels = array("a", "e", "i", "o", "u"); $cons = array("b", "c", "d", "g", "h", "j", "k", "l", "m", "n", "p", "r", "s", "t", "u", "v", "w", "tr", "cr", "br", "fr", "th", "dr", "ch", "ph", "wr", "st", "sp", "sw", "pr", "sl", "cl"); $num_vowels = count($vowels); $num_cons = count($cons); for($i = 0; $i < $length; $i++){ $password .= $cons[rand(0, $num_cons - 1)] . $vowels[rand(0, $num_vowels - 1)]; } return substr($password, 0, $length); } ?> -------------------------------------------------- <?php/** * The letter l (lowercase L) and the number 1 * have been removed, as they can be mistaken * for each other. */function createRandomPassword() { $chars = "abcdefghijkmnopqrstuvwxyz023456789"; srand((double)microtime()*1000000); $i = 0; $pass = '' ; while ($i <= 7) { $num = rand() % 33; $tmp = substr($chars, $num, 1); $pass = $pass . $tmp; $i++; } return $pass;}// Usage$password = createRandomPassword();echo "Your random password is: $password";?> ------------------------------------------------ <?php // Generate Random Password // ------------------ // MODES // 1 - (n)lowercase // 2 - (n)(lowercase + numbers) // 3 - (n)(lowercase + uppercase + numbers) // 4 - (n)lowercase + (n)numbers // 5 - (n)numbers only // ------------------ function passgen($len=4,$mode=4) { $chars=array(); $chars2=array(); if ($mode > 1){ // add numbers to $chars for($i=48;$i<=57;$i++) { array_push($chars, chr($i)); } } if ($mode==3){ // add uppercase to $chars for($i=65;$i<=90;$i++) { array_push($chars, chr($i)); } } if ($mode > 3){ // add lowercase to $chars2 for($i=97;$i<=122;$i++) { array_push($chars2, chr($i)); } }else{ // add lowercase to $chars for($i=97;$i<=122;$i++) { array_push($chars, chr($i)); } } if ($mode==4){ //build first half of password from $chars2 (lowercase) for($i=0;$i<$len;$i++) { mt_srand((double)microtime()*1000000); $passwd.=$chars2[mt_rand(0,(count($chars2)-1))]; } //build second half of password from $chars (numbers) for($i=0;$i<$len;$i++) { mt_srand((double)microtime()*1037800); $passwd.=$chars[mt_rand(0,(count($chars)-1))]; } }else{ // build password from $chars for($i=0;$i<$len;$i++) { mt_srand((double)microtime()*1000000); $passwd.=$chars[mt_rand(0,(count($chars)-1))]; } } return $passwd; } ?> ----------------------------------------- These are all i got from google search...... ----------------------- suresh |
![]() |
|
| Thread Tools | |
| Display Modes | |
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
| dervelop the website in php | rohit_iips | HTML & CSS | 5 | 07-11-2006 02:37 AM |
| Is PHP ready for Web 2.0? | Shivanna | HTML & CSS | 1 | 06-08-2006 09:14 PM |