what's best way to create a password generator?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
gilbertwang
Forum Commoner
Posts: 32
Joined: Sun Jun 30, 2002 11:08 pm
Location: Calgary

what's best way to create a password generator?

Post by gilbertwang »

This is quite a challenging task for me to do if you could give me some help that would be great.

I am trying to create a password generator after the user fills in the application form and click on the submit button.

It will generate a username and password with 8(numbers or characters),
once the user submits, it will send the username and password to the email specified by the registration and will insert the username and password into a new table in MYSQL but the customerid(primary key) will be the same as the registration details form.

What is the best way to do this? Is it better to use javascript to generate the username and password and then sends it to the user via the email and then insert to MYSQL database.

I know it is easier to insert the username and password to the same table as the registration details, but is it possible to insert into two tables having the same customerid(primary key).

Thanks to all php coder
User avatar
martin
Forum Commoner
Posts: 33
Joined: Fri Jun 28, 2002 12:59 pm
Location: Cambridgeshire

Post by martin »

This code will give you the password

Code: Select all

function generate_passwd($length = 50)
                 {
                 // all the chars we want to use
                 $all = explode(" ",
                 "a b c d e f g h j k m n p q r s t w x y z "
                 ."A B C D E F G H J K L M N P Q R S T W X Y Z "
                 ."2 3 4 5 6 7 8 9");

                 for($i=0;$i<$length;$i++)
                 &#123;
                 srand((double)microtime()*1000000);
                 $randy = rand(0, 61);
				 if ($all&#1111;$randy]!=" ")&#123;
                 $pass .= $all&#1111;$randy];
				  if (strlen($pass)>7)&#123;
				 
                 return $pass;
                 &#125;&#125;&#125;
                 &#125;
I'm sure someone will come up with something better (but in the mean time).

If you have an AUTO_INCREMENT column in your table, when you insert a new row, it's automatically assigned a new number. To get the number just created

$last_id = mysql_insert_id($link_id);

Where $link_id is what's returned from mysql_connect(..).
User avatar
kenny
Forum Newbie
Posts: 12
Joined: Mon Jul 01, 2002 10:12 pm
Location: Florida

Post by kenny »

http://mattford.net , has a great password generator script....

Code: Select all

<?php
function gen_pass($length=7)
&#123;
     $alpha = array(a,b,c,d,e,f,g,h,i,j,k,l,m,n,o,p,q,r,s,t,u,v,w,x,y,z,0,1,2,3,4,5,6,7,8,9);
     while($i < $length)
     &#123;
          $code .= $alpha&#1111;rand(0,count($alpha))];
          $i++;
     &#125;
     trim($code);
     return $code;
&#125;
?>
For example you could use it like this:
<?php
$password = gen_pass(10);
echo "Your password is $password";
?>
Post Reply