Jump to content
Updating IPB tonight Read more... ×

Archived

This topic is now archived and is closed to further replies.

proctk

Tutorial: Creating a Membership System with PHP and MySQL

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]

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites

×

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.