Solved

Create a temporary password using given characters

Posted on 2008-06-26
5
294 Views
Last Modified: 2010-05-18
I want to be able to create a password using given characters and length.

i.e.
$Characters = "abcGTH123_%&$";
$Length = 8;

PHP code to do it

Any ideas?
0
Comment
Question by:dnotestine
5 Comments
 
LVL 5

Expert Comment

by:codeQuantum
Comment Utility

<?php
 

$pass = "";

$Characters = "abcGTH123_%&$";

srand((double)microtime()*1000000);

$i = 0;

while ($i <= 8) {

	$num = rand() % strlen($Characters);

	$tmp = substr($Characters, $num, 1);

	$pass = $pass . $tmp;

	$i++;

}

// see result

echo $pass;

?>

Open in new window

0
 
LVL 9

Accepted Solution

by:
Rob Siklos earned 200 total points
Comment Utility

$numSourceChars = strlen($Characters);

$result = "";

for ($i = 0; $i < $Length; $i++) {

    $result .= substr($Characters, rand(1, $numSourceChars)-1, 1);

}

Open in new window

0
 
LVL 5

Assisted Solution

by:codeQuantum
codeQuantum earned 100 total points
Comment Utility
Note that the script is a refined version of the one found here :

http://www.issociate.de/board/post/184271/Random_Character_String.html

Fixed a small mistake with length :
<?php
 

// configuration

$Characters = "abcGTH123_%&$";

$Length = 8;
 

// generation

$pass = "";

srand((double)microtime()*1000000);

$i = 0;

while ($i < $Length) {

	$num = rand() % strlen($Characters);

	$tmp = substr($Characters, $num, 1);

	$pass = $pass . $tmp;

	$i++;

}

// see result

echo $pass;
 

?>

Open in new window

0
 
LVL 4

Assisted Solution

by:pdd
pdd earned 200 total points
Comment Utility

function generatePass ($length = 8, $chars = "abcGTH123_%&$") {

	$string = "";

	for ($i=0;$i<$length;$i++) {

		$string .= $chars[rand(0, strlen($chars)-1)];

	}

	return $string;

}
 

print generatePass();

Open in new window

0
 

Author Closing Comment

by:dnotestine
Comment Utility
Thanks to everyone - All were good examples
0

Featured Post

How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

Join & Write a Comment

Deprecated and Headed for the Dustbin By now, you have probably heard that some PHP features, while convenient, can also cause PHP security problems.  This article discusses one of those, called register_globals.  It is a thing you do not want.  …
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to count occurrences of each item in an array.

763 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now