Solved

Create a temporary password using given characters

Posted on 2008-06-26
5
295 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
ID: 21876792

<?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
ID: 21876811

$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
ID: 21876845
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
ID: 21884296

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
ID: 31471066
Thanks to everyone - All were good examples
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Things That Drive Us Nuts Have you noticed the use of the reCaptcha feature at EE and other web sites?  It wants you to read and retype something that looks like this.Insanity!  It's not EE's fault - that's just the way reCaptcha works.  But it is …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
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 create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

919 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

17 Experts available now in Live!

Get 1:1 Help Now