Page 1 of 1

what's best way to create a password generator?

Posted: Tue Jul 09, 2002 12:02 am
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

Posted: Tue Jul 09, 2002 5:00 am
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(..).

Posted: Tue Jul 09, 2002 9:06 am
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";
?>