Current location: Hot Scripts Forums » Programming Languages » PHP » [SOLVED] Random passwords


[SOLVED] Random passwords

Reply
  #1 (permalink)  
Old 05-18-08, 05:03 AM
myslowquietlife's Avatar
myslowquietlife myslowquietlife is offline
Newbie Coder
 
Join Date: Mar 2008
Location: London
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
[SOLVED] Random passwords

Hi all! (this is not a script request)

Hope your all well?

I have a members area and i need to have a forgotten password link.

Now i realise that i cant just send the passwords to the members as they are encrypted using md5 (and i wouldn't want to anyway). Im not asking for a script here but a link to a simple tutorial that will help me generate a random password and email it to the member in question.

Thanks very much.

Ben
__________________
"It's not the strong that survive, nor is it the intelligent, but those that can adapt to change"
Reply With Quote
  #2 (permalink)  
Old 05-18-08, 05:18 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
A random password generator is very simple actually. There are 2 "types" of random passwords: a series of random characters placed together in a string, or a string of combined existing words and digits. The former is probably the easiest way to create (it only takes 1 line of code), but the latter one is the easiest to use for the user who forgot his password (you try to remember a completely random 16-char password )

Here's how you can create a password with random characters:
PHP Code:

/**
* Generates a random password, using random characters
*
* @param int The length of the new random password
* @param array An array containing the characters that are 
                to be used in the password
* @return string The random password
*/
function generateCharacterPassword ($length$charRange=NULL) {
  if (
is_null ($charRange))
    
// use characters a-z, A-Z and 0-9
    
$charRange array_merge (range ('a''z'), range ('A''Z'), range (,9));

  
// generates the password:
  
$password NULL;
  while (
$length 0) {
    
// add a random character from the $charRange array to the password
    
$password .= $charRange[array_rand ($charRange)];
    
$length--;
  }
  return 
$password;

To use it:
PHP Code:

// creates a password of length 32
generateCharacterPassword (32);

// creates a password of length 5 with the characters a, b and c
generateCharacterPassword (5, array ('a''b''c')); 
To generate a password that contains known words, you'll need to create an array of known words, and pick a random item from that array. Basically it's the same as the previous code, except you'll be using an array that contains words instead of characters
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #3 (permalink)  
Old 05-18-08, 05:32 AM
myslowquietlife's Avatar
myslowquietlife myslowquietlife is offline
Newbie Coder
 
Join Date: Mar 2008
Location: London
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
excellent! well thank you very much it really is very easy!

now once this code has generated the password for this user how would i use it to reset the password on their database row.

Again a tutorial for this would be fine but thanks for giving the code example.

Ben

By the way i have only just mastered how to update/edit database using forms so i need this kinda simple
__________________
"It's not the strong that survive, nor is it the intelligent, but those that can adapt to change"
Reply With Quote
  #4 (permalink)  
Old 05-18-08, 06:02 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Quote:
Originally Posted by myslowquietlife View Post
By the way i have only just mastered how to update/edit database using forms so i need this kinda simple
That's the only thing you need

Since you want to reset a user's password, you already know his username (or login name, or whatever), so you should be able to access the user's information in the database.

Now the only thing you need to do is update the password field in the database that refers to the user of which you have the username.
You probably store it as an md5, so you'll have to hash the random-generated password first:
PHP Code:

$password generateCharacterPassword (10);

$hashedPassword md5 ($password); 
Now update the password field by setting the $hashedPassword and send the $password to the user in a mail or somthing.
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
  #5 (permalink)  
Old 05-18-08, 06:10 AM
myslowquietlife's Avatar
myslowquietlife myslowquietlife is offline
Newbie Coder
 
Join Date: Mar 2008
Location: London
Posts: 87
Thanks: 0
Thanked 0 Times in 0 Posts
ahhh thank you very much its all clear now.

I was thinking i might have to have a new field in the database with the new password (i read that somewhere and it just didnt seem right although i suppose thats one way of doing this).

Well thank you very much for the help UnrealEd

Ben
__________________
"It's not the strong that survive, nor is it the intelligent, but those that can adapt to change"
Reply With Quote
  #6 (permalink)  
Old 05-18-08, 07:39 AM
UnrealEd's Avatar
UnrealEd UnrealEd is offline
Community Liaison
 
Join Date: May 2005
Location: Antwerp, Belgium
Posts: 3,165
Thanks: 4
Thanked 25 Times in 25 Posts
Quote:
Originally Posted by myslowquietlife View Post
I was thinking i might have to have a new field in the database with the new password (i read that somewhere and it just didnt seem right although i suppose thats one way of doing this).
It is possible to add an extra field: most sites offer the option to reset the password back to it's original value after a couple of hours when you don't reply to their mail or visit the link in the mail.
They add this to make sure only the owner of the account has the option to reset his password.
What I told you in my previous post will work, but now I would be able to reset the password of anyones acount, eventhough they don't want to (I would have to know their email, but that's rather easy to find).

Basically what those sites do (and what I would do) is store the new password in another field than the one you store the old password of the user. You then send a link via mail to the user that refers to a page that will overwrite the old password with the new password, and then let the user fill in his new password.

Quote:
Originally Posted by myslowquietlife View Post
Well thank you very much for the help UnrealEd

Ben
Anytime
__________________
"Good judgement comes from experience, and experience comes from bad judgement." - Fred Brooks

Reply With Quote
Reply

Bookmarks


Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
random image with random text/quote gx10vn PHP 4 10-22-04 08:30 AM
Random PHP include brduran PHP 4 08-16-04 06:34 AM
picking random entries with a filter... Double selection problem dsumpter PHP 7 11-16-03 07:19 PM
Searching for a random quote free script... Sgrebs Script Requests 0 11-14-03 02:44 PM


All times are GMT -5. The time now is 07:43 AM.
vBulletin® Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.