Solved

Create a temporary password using given characters

Posted on 2008-06-26
5
297 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Wordpress Only run code if on a certain page 11 35
Multi line FPDF footer: 3 27
Make check boxes work 8 46
What is define("__APPROOT__", __DIR__); 6 32
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

749 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