In order for us to answer we will need to know a bit more about your registration form. Do you allow your user to enter the password or do you always want a generated password?
Thread: Random password generation |
I want to generate password randomly and send to mysql database using PHP..i have got the following function for generating password but doesn't know how to incorporate in my registration form and where to call that function and update password field in database..please help me..
where should i call this function generatePassword????Code:function generatePassword ( $length = 8 ) {
// start with a blank password
$password = “”;
// define possible characters
$possible = “0123456789abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ!@#$%^&*”;
// set up a counter
$i = 0;
// add random characters to $password until $length is reached
while ($i < $length) {
// pick a random character from the possible ones
$char = substr($possible, mt_rand(0, strlen($possible)-1), 1);
// we don’t want this character if it’s already in the password
if (!strstr($password, $char)) {
$password .= $char;
$i++;
}
}
// done!
return $password;
}
In order for us to answer we will need to know a bit more about your registration form. Do you allow your user to enter the password or do you always want a generated password?
If you are always generating a password you should just have to call that function.
Can you show us the registration page?
I am not allowing the user to enter the password.i want to generate it and send it to user's email.my registration form is:
Code:<form name="account" method="post"> <div><h2>User Account</h2></div> <fieldset><legend><b>Account Information</b></legend> <div>Username<span style="color:#FF0000;">*</span></div> <div><input type="text" name="un" id="un" style="width:300px;" /></div> <div style="font-size:11px;">Spaces are allowed; punctuation is not allowed except for periods, hyphens, and underscores.</div> <div>E-mail Address<span style="color:#FF0000;">*</span></div> <div><input type="text" name="email" id="email" style="width:300px;" /></div> <div style="font-size:11px;">A valid e-mail address. All e-mails from the system will be sent to this address. The e-mail address is not made public and will only be used if you wish to receive a new password or wish to receive certain news or notifications by e-mail.</div> </fieldset> <fieldset><legend><b>Personal Information</b></legend> <div>First Name<span style="color:#FF0000;">*</span></div> <div><input type="text" name="fn" id="fn" style="width:300px;"/></div> <div>Last Name<span style="color:#FF0000;">*</span></div> <div><input type="text" name="ln" id="ln" style="width:300px;" /></div> <div style="font-size:11px;">The content of this field is kept private and will not be shown publicly.</div> <div>Company Name</div> <div><input type="text" name="cn" id="cn" style="width:300px;"/></div> <div>Street Address<span style="color:#FF0000;">*</span></div> <div><input type="text" name="add" id="add" style="width:300px;" /></div> <div style="font-size:11px;">The content of this field is kept private and will not be shown publicly.</div> <div>City<span style="color:#FF0000;">*</span></div> <div><input type="text" name="cty" id="cty" style="width:300px;" /></div> <div>State<span style="color:#FF0000;">*</span></div> <div><input type="text" name="state" id="state" style="width:300px;" /></div> <div>Postal Code<span style="color:#FF0000;">*</span></div> <div><input type="text" name="pin" id="pin" style="width:300px;" /></div> <div style="font-size:11px;">The content of this field is kept private and will not be shown publicly.</div> <div>Phone Number<span style="color:#FF0000;">*</span></div> <div><input type="text" name="phone" id="phone" style="width:300px;" /></div> <div style="font-size:11px;">The content of this field is kept private and will not be shown publicly.</div> </fieldset> <div><input type="submit" name="submit" id="submit" value="Create New Account" /></div> </form>
Have you written any PHP code besides that function?
i have written the insert query as follows:What should i insert against pw field and where should i write and call the generate password function..??Code:if(isset($_POST['submit']))
{
$insert=mysql_query("INSERT INTO dress_user(username, email, firstname, lastname, company, address, city,
state, pin, phone, password)VALUES('$_POST[un]',
'$_POST[email]',
'$_POST[fn]',
'$_POST[ln]',
'$_POST[cn]',
'$_POST[add]',
'$_POST[cty]',
'$_POST[state]',
'$_POST[pin]',
'$_POST[phone]',
' ')") or die("Query failed : " . mysql_error());
}
Last edited by WingedPanther; 06-17-2009 at 07:41 AM. Reason: add code tags (the PHP sheet)
hey thanks for all ur efforts...i have written that function in other php file and included it in the current registration file as follows:
$pw=generatePassword(8);
and inserted $pw in against the password field...
thanks this script was really very useful.
There are currently 1 users browsing this thread. (0 members and 1 guests)