Geeks Talk

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.

Generate random password Using PHP

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

Go Back   Geeks Talk > Web Development > PHP
Register Blogs FAQ Tag Cloud Calendar Mark Forums Read

PHP PHP - The most popular scripting language. Discuss PHP related questions here. Sample Scripts, Popular downloads, tools and utilites and more...

Reply

 

LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 06-22-2006
Contributing Member
 
Join Date: May 2006
Posts: 79
Thanks: 0
Thanked 6 Times in 5 Posts
fred is on a distinguished road
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
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 12-14-2006
Contributing Member
 
Join Date: Sep 2006
Location: bangalore, india
Posts: 1,016
Thanks: 0
Thanked 89 Times in 72 Posts
psuresh1982 will become famous soon enough
Smile Re: Generate random password Using PHP

<?
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) {
&nbsp; $chars=array();
&nbsp; $chars2=array();
&nbsp; if ($mode > 1){
&nbsp; &nbsp; // add numbers to $chars
&nbsp; &nbsp; for($i=48;$i<=57;$i++) {
&nbsp; &nbsp; &nbsp; array_push($chars, chr($i));
&nbsp; &nbsp; }
&nbsp; }
&nbsp; if ($mode==3){
&nbsp; &nbsp; // add uppercase to $chars
&nbsp; &nbsp; for($i=65;$i<=90;$i++) {
&nbsp; &nbsp; &nbsp; array_push($chars, chr($i));
&nbsp; &nbsp; }
&nbsp; }
&nbsp; if ($mode > 3){
&nbsp; &nbsp; // add lowercase to $chars2
&nbsp; &nbsp; for($i=97;$i<=122;$i++) {
&nbsp; &nbsp; &nbsp; array_push($chars2, chr($i));
&nbsp; &nbsp; }
&nbsp; }else{

&nbsp; &nbsp; // add lowercase to $chars
&nbsp; &nbsp; for($i=97;$i<=122;$i++) {
&nbsp; &nbsp; &nbsp; array_push($chars, chr($i));
&nbsp; &nbsp; }
&nbsp; }
&nbsp; if ($mode==4){
&nbsp; &nbsp; //build first half of password from $chars2 (lowercase)
&nbsp; &nbsp; for($i=0;$i<$len;$i++) {
&nbsp; &nbsp; &nbsp; mt_srand((double)microtime()*1000000);
&nbsp; &nbsp; &nbsp; $passwd.=$chars2[mt_rand(0,(count($chars2)-1))];
&nbsp; &nbsp; }
&nbsp; &nbsp; //build second half of password from $chars (numbers)
&nbsp; &nbsp; for($i=0;$i<$len;$i++) {
&nbsp; &nbsp; &nbsp; mt_srand((double)microtime()*1037800);
&nbsp; &nbsp; &nbsp; $passwd.=$chars[mt_rand(0,(count($chars)-1))];
&nbsp; &nbsp; }
&nbsp; }else{
&nbsp; &nbsp; // build password from $chars
&nbsp; &nbsp; for($i=0;$i<$len;$i++) {
&nbsp; &nbsp; &nbsp; mt_srand((double)microtime()*1000000);
&nbsp; &nbsp; &nbsp; $passwd.=$chars[mt_rand(0,(count($chars)-1))];
&nbsp; &nbsp; }
&nbsp; }
&nbsp; return $passwd;
}
?>
-----------------------------------------

These are all i got from google search......

-----------------------
suresh
Reply With Quote
  #3 (permalink)  
Old 02-06-2007
Junior Member
 
Join Date: Jan 2007
Posts: 3
Thanks: 0
Thanked 1 Time in 1 Post
ravi_shekhar80 is on a distinguished road
Re: Generate random password Using PHP

or simply use this

$pass = md5(uniqid(rand(), true));
Reply With Quote
Reply

  Geeks Talk > Web Development > PHP

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


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


All times are GMT -4. The time now is 02:27 PM.


Powered by vBulletin® Version 3.8.4
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO 3.3.1
Copyright © 2009 GeekInterview.com. All Rights Reserved