WeberDev.com PHP and MySQL Code

LOG IN
BEGINNER GUIDES  |  PHP CLASSES  |  CODE SEARCH  |  ARTICLES SEARCH  |  PHP FORUMS  |  PHP MANUAL  |  PHP FUNCTIONS LIST  |  WEB SITE TEMPLATES
Start typing to search for PHP and MySQL Code Snippets and Articles Search
Submit a code Example / Snippet Submit Your Code
Search Engine Optimization Monitor SEO Monitor
Web Site UpTime Monitor UpTime Monitor
WeberDev's Monthly code contest PHP Code Contest
Your Personal Examples List My Favorite Examples
Your Personal Articles List My Favorite Articles
Edit Account Info Update Your Profile
PHP Code Search
Web Development Forums
Learn MySQL Playing Trivia
PHPBB2 Templates
Web Development Index
Web Development Resources
Web Development Content
PHPClasses
PHP Editor
PHP Jobs
Vision.To Design
Ajax Tutorials
PHP Programming Help
PHP/MySQL Programming
Webmaster Resources
Webmaster Forum
XML meta language
website builder
Mobile Dev World

Go Back Add a Comment Send this example to a friend Add this Article to your personal favoritest for easy future access to your favorite Code Examples and Articles. Submit a code example Print this code example.
BACK ADD A COMMENT SEND TO A FRIEND ADD TO MY FAVORITES ADD CODE EXAMPLES PRINT
Title : Yet another random password generator with md5 encryption
Categories : PHP, MySQL, Databases Click here to Update Your Picture
bastien koert
Date : Mar 02nd 2005
Grade : 3 of 5 (graded 9 times)
Viewed : 7629
File : No file for this code example.
Images : No Images for this code example.
Search : More code by bastien koert
Action : Grade This Code Example
Tools : My Examples List

  Submit your own code examples 
 

This little script replaces and emails out a new password to a user dumb enough to lose theirs. I have NEVER done this (yeah, right!).

So make your user's lives easier by allowing them to do this, just place a 'lost password' link to a page with this script and thats it. It also flags the account with a temporary password marker that would force the user to change their password when they next log on. Keep that or lose it, its up to you.


<?
//ep.php
/*
    resets and mails out a new temp password to the user
*/

session_start();
if (!isset(
$_SESSION['logged_in'])){
   
$_SESSION['logged_in'] = "";
}
//declarations
require("conn.php");
require(
"common.php");

global
$err_msg;
$err_msg = "";

//control code
if (isset($_POST['cbSend'])){
   
mail_password();
}else{
   
show_form();
}
//end if


//---------------------------------------------------------------------------------------
//                mail password function
//---------------------------------------------------------------------------------------

function mail_password()
{
    global
$err_msg;
   
//get the variables from the form
   
if ((isset($_POST['email']))&&(isset($_POST['lg_name']))){
       
$email = $_POST['email'];
       
$mid     = $_POST['lg_name'];
       
$date_cookie = $_COOKIE['last_time'];
    }else{
       
$err_msg = "<b>Please enter both your email address and your username. Thank you.</b>";
       
show_form();
        die();
    }
//end if
   
    //create the sql and run the query   
   
$sql = "SELECT * FROM users WHERE user_email='$email' and user_name = '$mid'";

   
$result = connect($sql);

   
//check the query results
   
if (mysql_num_rows($result)!=1){
       
$err_msg = "<font color=red>No results found. Please re-enter your username and email address to try again.</font>";
       
show_form();

    }else{
   
       
$row = mysql_fetch_array($result);
       
$email2 = $row['cust_email'];
       
$pass   = $row['cust_pw'];
   
       
//call the change password function and pass it the information related to the record to create the temp password
       
$new_pass = change_password($mid, $pass);
       
       
$sendto     = $email2;
       
$from     = "WebMaster <webmaster@domain.com>";
       
$subject    = "Forgotten Password";
       
$message    = "Dear $email2,
   
        Your password is $new_pass.
   
        Regards,
        Webmaster"
;
        echo
$message;
       
       
$headers = "MIME-Version: 1.0\n";
       
$headers .= "Content-type: text/plain; charset=iso-8859-1\n";
       
$headers .= "X-Priority: 3\n";
       
$headers .= "X-MSMail-Priority: Normal\n";
       
$headers .= "X-Mailer: php\n";
       
$headers .= "From: \"".$from."\" <".$from.">\n";
   
        if (!
mail($sendto, $subject, $message, $headers)){
            echo
"Mail failed to send";
        }else{
           
header("location:confirm1.htm");
        }
//end if
   
}//end if
}//end function

//---------------------------------------------------------------------------------------
//            change password function
//---------------------------------------------------------------------------------------
function change_password($id, $password)
{
   
//generate a random password
   
$pass = "";
   
$salt = "abchefghjkmnpqrstuvwxyz0123456789";
   
srand((double)microtime()*1000000);
   
$i = 0;
    while (
$i <= 7) {
       
$num = rand() % 33;
       
$tmp = substr($salt, $num, 1);
       
$pass = $pass . $tmp;
       
$i++;
    }
   
//change the password in the db
   
$sql = "update cust_info set cust_pw ='".md5($pass)."', temp_pass = 1 where cust_lg = '$id' and cust_pw = '$password'";
   
$result = connect($sql);
    if (
$result){     
        return
$pass;
    }else{       
       
change_password($id, $password);
    }
}
//end function

//---------------------------------------------------------------------------------------
//                show_form function
//---------------------------------------------------------------------------------------

function show_form()
{
    global
$err_msg;
   
html_header();
?>

<CENTER><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=1 WIDTH="540" >

<TR>
<TD><FORM method="POST" action="<? echo $_SERVER['PHP_SELF'];?>">
<CENTER><TABLE BORDER=0 CELLSPACING=0 CELLPADDING=3 WIDTH="549" HEIGHT="1" >
<TR>
<TD ALIGN=LEFT VALIGN=TOP >
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=1 WIDTH="500" >
<TR><TD><?echo $err_msg; ?></TD></TR>
<TR>
<TD><LEFT><FONT FACE="Arial,Helvetica"><FONT SIZE=-1>Enter your Member ID and Email. Your Password will be emailed to you.</FONT></FONT></LEFT></TD>
</TR>
</TABLE>
</TD>
</TR>
<TR>
<TD ALIGN=LEFT VALIGN=TOP>
<TABLE BORDER=0 CELLSPACING=0 CELLPADDING=0 COLS=2 WIDTH="500" >
<TR>
<TD ALIGN=LEFT VALIGN=TOP WIDTH="75"><LEFT><FONT FACE="Arial,Helvetica"><FONT SIZE=-1>Member ID</FONT></FONT></TD>

<TD><INPUT type="text" name="lg_name" size="40" value=""></LEFT></TD>
</TR>

<TR>
<TD ALIGN=LEFT VALIGN=TOP WIDTH="75"><LEFT><FONT FACE="Arial,Helvetica"><FONT SIZE=-1>Email</FONT></FONT></TD>

<TD><INPUT type="text" name="email" size="40" value=""></LEFT></TD>
</TR>
</TABLE>
<LEFT><INPUT type="submit" value="Send" name="cbSend"><INPUT type="button" value="Cancel" name="cbCancel" onclick="Javascript:history.back()"></LEFT></FORM></TD>
</TR>


<?
    html_footer
();
}
//end function
?>



bookmarker - PHP, PHPLIB, MySQL WWW based bookmark manager
Categories : MySQL, PHP, MySQL, Complete Programs, Databases
This program allows you to upload an ODBC ressource - i.e. an MS-Access database to a MySQL server.
Categories : Databases, MySQL, Complete Programs, PHP, Databases
A template script to provide the ability to get the next or previous n records from a MySQL database.
Categories : Databases, PHP, MySQL
Cut your MySQL Connections to 1 line of code
Categories : PHP, Beginner Guides, Databases, MySQL
Returns the last row from a select where multiple rows have been selected. In this case it is used to determine the last number in an AUTO_INCREMENT column. I use it to create "ticket numbers" or anything that needs a unique ID in sequence.
Categories : mSQL, MySQL, Databases, PHP
MySQL Connection/Query Class
Categories : Databases, MySQL, PHP, PHP Classes
Complex paging with no resultset limit
Categories : PHP, MySQL, Databases, Output Control, HTML and PHP
Phorum, MySQL, Language, UK date format, MySQL UK Date format
Categories : PHP, Date Time, Strings, MySQL, Databases
BBS system for easy customization. Utilizes mySQL.
Categories : Complete Programs, MySQL, PHP, Databases
Broadcast HTML Email
Categories : PHP, Email, MySQL, Databases
Functions for loading images into a MySQL database and displaying them.
Categories : Graphics, HTML and PHP, MySQL, PHP, Databases
for each record, do this to the first record, and do that to any subsequent record
Categories : PHP, Databases, MySQL, Beginner Guides
PHP4 AND MySQL Authentication
Categories : PHP, MySQL, Authentication, Databases
phpFormGenerator for Dynamic Form Generation from MySQL
Categories : PHP, PHP Classes, MySQL, Databases, HTML and PHP
Zip code range and distance calculation class v1.0.0
Categories : PHP, Databases, MySQL, Zip Code