Jump to content

Tutorial: Creating a Membership System with PHP and MySQL


proctk

Recommended Posts

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)

[code=php:0]

<?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';
}
?>
[/code]

Link to comment
Share on other sites

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


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

Orio.
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.