Jump to content


Photo

Tutorial: Creating a Membership System with PHP and MySQL


  • Please log in to reply
2 replies to this topic

#1 proctk

proctk
  • Members
  • PipPipPip
  • Advanced Member
  • 300 posts

Posted 23 July 2006 - 06:23 PM

Hi

I have been working through the above tutorial. I have been able to get everything to work except the password recovery utility.

I created the form see below

any thought why this is not working



<body>
<form id="form1" name="form1" method="post" action="codeLostPassword.php">
  <label for="textfield">Email Address:</label>
  <input type="text" name="email_address" id="email_address" />
  <input name="hiddenField" type="hidden" value="recover" />
 
  <input type="submit" name="Submit" value="Submit" id="Submit" />
</form>

</body>



and made a few changes to the code for regenerating the password. (file names only)


<?php session_start(); ?>

<?
include 'db.php';

switch($_POST['recover']){
    default:
    include 'lost_pw.php';
    break;
    
    case "recover":
    recover_pw($_POST['email_address']);
    break;
}
function recover_pw($email_address){
    if(!$email_address){
        echo "You forgot to enter your Email address 
            <strong>Knucklehead</strong><br />";
        include 'lost_pw.php';
        exit();
    }
    // quick check to see if record exists    
    $sql_check = mysql_query("SELECT * FROM users WHERE email_address='$email_address'");
    $sql_check_num = mysql_num_rows($sql_check);
    if($sql_check_num == 0){
        echo "No records found matching your email address<br />";
        include 'lost_pw.php';
        exit();
    }
    // Everything looks ok, generate password, update it and send it!
    
    function makeRandomPassword() {
          $salt = "abchefghjkmnpqrstuvwxyz0123456789";
          srand((double)microtime()*1000000); 
          $i = 0;
          while ($i <= 7) {
                $num = rand() % 33;
                $tmp = substr($salt, $num, 1);
                $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 MyWebsite!";
    $message = "Hi, we have reset your password.
    
    New Password: $random_password
    
    http://www.mywebsite.com/login.php
    
    Thanks!
    The Webmaster
    
    This is an automated response, please do not reply!";
    
    mail($email_address, $subject, $message, "From: MyDomain Webmaster<admin@mydomain.com>\n
        X-Mailer: PHP/" . phpversion());
    echo "Your password has been sent! Please check your email!<br />";
    include 'login.php';
}
?> 



#2 Orio

Orio
  • Staff Alumni
  • Advanced Member
  • 2,491 posts

Posted 23 July 2006 - 06:30 PM

The problem is here:
switch($_POST['recover']){
    default:
    include 'lost_pw.php';
    break;
    case "recover":
    recover_pw($_POST['email_address']);
    break;


$_POST['recover'] Should be $_POST['hiddenField']
Also, change- "if(!$email_address){" to "if(empty($email_address)){"

Orio.
Think you're smarty?

(Gone until 20 to November)

#3 proctk

proctk
  • Members
  • PipPipPip
  • Advanced Member
  • 300 posts

Posted 23 July 2006 - 06:39 PM

Thank you very much, that solved my problem




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users