PHP Forgot Password script

This is a discussion on "PHP Forgot Password script" within the PHP Forum section. This forum, and the thread "PHP Forgot Password script are both part of the Program Your Website category.


 Subscribe in a reader

Go Back   Webforumz.com > Main Forums > Program Your Website > PHP Forum

Notices




Reply
 
LinkBack Thread Tools
  #1  
Old Aug 24th, 2006, 06:14
New Member
Join Date: Aug 2006
Location: damaguete city
Age: 28
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
PHP Forgot Password script

Im a newbie in PHP programming can anybody help me, just a simple PHP forgot password script and PHP change password.. any idea

thanks

chrizlord

Last edited by chrizlord; Aug 26th, 2006 at 01:00.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote

  #2  
Old Aug 30th, 2006, 20:36
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

First off you should not be able to see the password because it's just that!

So if the user forgets their password, what you should do is generate them a new temporary login which they then use to set their new password.

A login script needs to process the form information, check the id and the MD5 hashed password against that held in the database and if they match, let the user in.

The change password script is almost identical except that you require the user to enter both the old and the new password. Only if the old password, when given the MD5 treatment, matches that held in the database, do you accept the new password, give it the MD5 treatment and store it in the database.

Lots of info can be found on http://php.net
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #3  
Old Aug 31st, 2006, 02:37
New Member
Join Date: Jul 2006
Location: India
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

Text deleted

Last edited by ukgeoff; Aug 31st, 2006 at 09:31. Reason: Do not advertise your services within the thread.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #4  
Old Aug 31st, 2006, 11:37
Tim356's Avatar
Reputable Member
Join Date: Nov 2003
Location: Australia
Age: 26
Posts: 331
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

So extending on what ukgeoff had to say:

The forgot password script
A form where the user can enter their username and email, when submitted the username and email would be compared to what's in the database, if correct a new password would be set for them and sent to their email address. You could also have a field in the database to mark this password as temporary - so they have to change it as soon as they first login with it.

The change password script
A from with 3 fields - old password, new password and confirm new password. When submitted, the old password (which would be secure hash algorythm (sha 1)encrypted - 160-bit as opposed to md5's 128-bit, so harder to crack), would be compared with the password stored in the database - if passwords match then the 2 new passwords would be compared. If the 2 new passwords match, then the new password would be encrypted (sha 1 again) and stored in the database.

Let me know if you want examples.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #5  
Old Sep 4th, 2006, 00:48
New Member
Join Date: Aug 2006
Location: damaguete city
Age: 28
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

helo

good day thanks for reply guys i rely appreciate it... can u give me some examples for forgot password and change password...

thanks
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #6  
Old Sep 14th, 2006, 22:10
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

This is my fogot password script.
The user has to enter their email address and a new password is generated and sent to their email address

PHP: Select all

<?php
session_start
();  // Start Session
session_register("session");
// This is displayed if all the fields are not filled in
$empty_fields_message "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
// Convert to simple variables  
$email_address $_POST['email_address'];
if (!isset(
$_POST['email_address'])) {
?>
<h2>Recover a forgotten password!</h2>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <p class="style3"><label for="email_address">Email:</label>
    <input type="text" title="Please enter your email address" name="email_address" size="30"/></p>
    <p class="style3"><label title="Reset Password">&nbsp</label>
    <input type="submit" value="Submit" class="submit-button"/></p>
</form>
<?php
}
elseif (empty(
$email_address)) {
    echo 
$empty_fields_message;
}
else {
$email_address=mysql_real_escape_string($email_address);
$status "OK";
$msg="";
//error_reporting(E_ERROR | E_PARSE | E_CORE_ERROR);
if (!stristr($email_address,"@") OR !stristr($email_address,".")) {
$msg="Your email address is not correct<BR>"
$status"NOTOK";}

echo 
"<br><br>";
if(
$status=="OK"){  $query="SELECT email_address,username FROM users WHERE users.email_address = '$email_address'";
$st=mysql_query($query);
$recs=mysql_num_rows($st);
$row=mysql_fetch_object($st);
$em=$row->email_address;// email is stored to a variable
 
if ($recs == 0) {  echo "<center><font face='Verdana' size='2' color=red><b>No Password</b><br> Sorry Your address is not there in our database . You can signup and login to use our site. <BR><BR><a href='http://www.jackgodfrey.org.uk/register'>Register</a> </center>"; exit;}
function 
makeRandomPassword() { 
          
$salt "abchefghjkmnpqrstuvwxyz0123456789"
          
srand((double)microtime()*1000000);  
          
$i 0
          while (
$i <= 7) { 
                
$num rand() % 33
                
$tmp substr($salt$num1); 
                
$pass $pass $tmp
                
$i++; 
          } 
          return 
$pass
    } 
    
$random_password makeRandomPassword(); 
    
$db_password md5($random_password); 
     
    
$sql mysql_query("UPDATE users SET password='$db_password'  
                WHERE email_address='$email_address'"
); 
     
    
$subject "Your password at www.yoursite.com"
    
$message "Hi, we have reset your password. 
     
    New Password: $random_password 
     
    http://www.yoursite.com/login
    Once logged in you can change your password 
     
    Thanks! 
    Site admin 
     
    This is an automated response, please do not reply!"

     
    
mail($email_address$subject$message"From: yoursite.com Webmaster<admin@jyoursite.com>\n 
        X-Mailer: PHP/" 
phpversion()); 
    echo 
"Your password has been sent! Please check your email!<br />"
    echo 
"<br><br>Click <a href='http://www.yoursite.com/login'>here</a> to login";
 } 
 else {echo 
"<center><font face='Verdana' size='2' color=red >$msg <br><br><input type='button' value='Retry' onClick='history.go(-1)'></center></font>";}
}
?>
Here is my change password script
PHP: Select all

<?
session_start
();
session_register("session");
//if(!isset($session['userid'])){
//echo "<center><font face='Verdana' size='2' color=red>Sorry, Please login and use this page </font></center>";
//exit;
//}
// This is displayed if all the fields are not filled in
$empty_fields_message "<p>Please go back and complete all the fields in the form.</p>Click <a class=\"two\" href=\"javascript:history.go(-1)\">here</a> to go back";
// Convert to simple variables 
$password1 $_POST['password1']; 
$password2 $_POST['password2'];
if (!isset(
$_POST['password1'])) {
?>
<h2>Change password! <? echo $_SESSION['email_address']; ?></h2>
<form method="post" action="<?php echo $_SERVER['REQUEST_URI']; ?>">
    <p class="style3"><label for="password1"">New password:</label>
    <input type="password" title="Please enter a password" name="password1" size="30"></p>
    <p class="style3"><label for="password2">Re-enter Password:</label>
    <input type="password" title="Please re-enter password" name="password2" size="30"></p>
    <p style="stext-align:left"><label for="submit">&nbsp</label>
    <input type="submit" value="Change" class="submit-button"/></p>
</form>
<?php
}
elseif (empty(
$password1) || empty($password2))  {
    echo 
$empty_fields_message;
}
else {
include 
'includes/connection.php'
$db_password1=md5(mysql_real_escape_string($password1));
//Setting flags for checking
$status "OK";
$msg="";
if ( 
strlen($password1) < or strlen($password1) > 10 ){
$msg=$msg."Password must be more than 3 characters in length and maximum 10 characters in length<BR>";
$status"NOTOK";}     
if (
strcmp$password1,$password2 ) !=0){
$msg=$msg."Both passwords do not match<BR>";
$status"NOTOK";}     
if(
$status<>"OK"){ 
echo 
"<font face='Verdana' size='2' color=red>$msg</font><br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";
}else{ 
// if all validations are passed.
if(mysql_query("update users set password='$db_password1' where userid='$session[userid]'")){
echo 
"<font face='Verdana' size='2' ><center>Thanks <br> Your password changed successfully. Please keep changing your password for better security</font></center>"$password1;
}
}
}
?>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #7  
Old Sep 15th, 2006, 11:20
Most Reputable Member
Join Date: Apr 2006
Location: Cornwall, UK
Posts: 1,310
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

I think I tried to make the point before but I'll restate it.

Do not save passwords in an 'as is' format. Always use the crypt() function to create a one-way password. This is what you store.

When a person is required to enter a password, you take the password they have entered and the stored password, feed them into the crypt() function and if the value returned is the same as stored, then the password is valid.

If you want to send a temp password to someone who has forgotten theirs or is registering for the first time, here's the code I use. It generates an 8 character mix of upper and lowercase letters.
Code: Select all
            function genPswd() {
                for ($i=1; $i<=8; $i++) {
                   $goUpper = (rand(0,1) == 1);
                  $char = chr(rand(97, 122));
                  if ($goUpper){
                      $char = strtoupper($char);
                  }
                  $pass .= $char;
               }
               return $pass;
            }
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #8  
Old Sep 15th, 2006, 11:55
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

I used md5 to encrypt my passwords so the user can choose their password and it gets encrypted to a 32 character length password which is still valid.

With my forgot password the user is sent new random password which they can change to one they can remember and it get's encrytped using md5
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #9  
Old Sep 19th, 2006, 09:20
New Member
Join Date: Aug 2006
Location: damaguete city
Age: 28
Posts: 3
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

hello gud day!

thank you adrock and ukgeoff for the reply it realy helps me alot ive an idea already.. have a nice day .. GOD BLESS
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #10  
Old Apr 25th, 2007, 02:14
Up'n'Coming Member
Join Date: Apr 2007
Location: Canada
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

Hi adRock,

Thank you first for the scripts. It is the one that I was looking for but only little problem is that after receiving the mail with new password when user log on to the site with the new password, it says that it can not find the user with specified password. Any idea what it might cause. Does it has any thing to do with database field propery? Do I need to make that filed as md5. I tried though but it still did not work. Your response would be much appreciated.
Thanks again.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #11  
Old Apr 30th, 2007, 23:20
Highly Reputable Member
Join Date: Jul 2006
Location: Devon, England
Posts: 565
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

You need to change thge fields in your database

I will have a look at my database and see what fields are what and let you know.

What fields have you set up in your database so i can compare

Last edited by AdRock; Apr 30th, 2007 at 23:23.
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
  #12  
Old May 1st, 2007, 00:19
Up'n'Coming Member
Join Date: Apr 2007
Location: Canada
Posts: 88
Thanks: 0
Thanked 0 Times in 0 Posts
Re: PHP Forgot Password script

But I think it is the login scripts that causing the trouble. Because it take the entered password from the user and does not convert to MD5 format when check with the database. So if you are so kind enough to post your login script so that I can get a better idea. I am posting my script see if you can find out any problem. But I am pretty sure it is my login scripts. I am giving it here.......

Code: Select all
include "include/session.php";
include "include/z_db.php";
//////////////////////////////

?>
<!doctype html public "-//w3c//dtd html 3.2//en">
<html>
<head>
<title>(Type a title for your page here)</title>
<meta name="GENERATOR" content="Arachnophilia 4.0">
<meta name="FORMATTER" content="Arachnophilia 4.0">
</head>
<body bgcolor="#ffffff" text="#000000" link="#0000ff" vlink="#800080" alink="#ff0000">
<?
$userid=mysql_real_escape_string($userid);
$password=mysql_real_escape_string($password);
if($rec=mysql_fetch_array(mysql_query("SELECT * FROM tbl_login WHERE userid='$userid' AND password = '$password'"))){
 if(($rec['userid']==$userid)&&($rec['password']==$password)){
  include "include/newsession.php";
            echo "<p class=data> <center>Successfully,Logged in<br><br><a href='logout.php'> Log OUT </a><br><br><a href=welcome.php>Click here if your browser is not redirecting automatically or you don't want to wait.</a><br></center>";
     print "<script>";
       print " self.location='welcome.php';"; // Comment this line if you don't want to redirect
          print "</script>";
    } 
  } 
 else {
  session_unset();
echo "<font face='Verdana' size='2' color=red>Wrong Login. Use your correct  Userid and Password and Try <br><center><input type='button' value='Retry' onClick='history.go(-1)'></center>";
  
 }
?>

</body>
</html>
Digg this Post!Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!Spurl this Post!Reddit! Wong this Post!
Reply With Quote
Reply

Tags
php, forgot, password, script

Thread Tools

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 On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

Similar Threads
Thread Thread Starter Forum Replies Last Post
Forgot password and Change password PHP script Chono PHP Forum 4 May 16th, 2008 09:13
Password script needs refining. liwolfbyte JavaScript Forum 4 Sep 3rd, 2007 20:05
Best script to create users for password protected pages? vandiermen PHP Forum 3 Apr 23rd, 2007 12:22
PHP Forgot password script error eddie PHP Forum 4 Mar 4th, 2007 15:43
Looking for EASY password protect script Lchad PHP Forum 3 Jan 28th, 2007 00:54


All times are GMT. The time now is 03:16.


Powered by vBulletin®
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.2.0 RC8
© 2003-2008 Webforumz.com : All Rights Reserved

1 2 3 4 5 6 7 8 9 10 11