Results 1 to 9 of 9

Thread: Day 16 - Forget Password Feature - Become PHP Expert in 30 days

  1. #1
    Join Date
    Sep 2003
    Posts
    3,040

    Default Day 16 - Forget Password Feature - Become PHP Expert in 30 days

    Forget Password Feature

    In Day 15, we created signup page for our application. Since we take a long break, many of you have forget the password used to signup.

    So lets make a forget password feature.

    You may delete everything from server and keep Day 15 programs, so it will be less cluttered. Now onwards we will be doing an application to manage TODO list.

    Home Page For our TODO Application

    Lets create a very basic HTML page as our home page. We can make it look better, for now, this is a skeleton.

    index.html

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
        <title></title>
    </head>
    <body>
    
    <h1>ToDo Application</h1>
    
    <a href="signup.html">Signup</a><br>
    <a href="lost_password.html">Lost Password</a><br>
    
    </body>
    </html>
    This page links to 2 HTML pages. signup.html and lost_password.html. Both will need PHP files with same name.

    signup.html and signup.php are covered in Day 15.

    Today we will make lost_password.html and lost_password.php

    Lost Password

    If user forget his password, can get password using lost password feature. This is done in 2 parts to make it simple.

    lost_password.html

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
        <title>Lost Password</title>
    </head>
    <body>
    
    <h1>Lost Password</h1>
    
    <form method="post" action="lost_password.php">
        Enter Email<br>
        <input type="text" name="email"><br>
        <button type="submit" name="submit">Get My Password</button>
    </form>
    
    </body>
    </html>
    This is simple HTML form page. User can input his email address used to create account. When user click the submit button. we pass the FORM to lost_password.php using post method.

    lost_password.php

    This PHP will read email address user submitted from $_POST variable. Then connect to database, find user having the email address.

    PHP Code:
    <?php

    if (isset($_POST['email'])) {
        
    $email $_POST['email'];
    } else {
        die(
    'Please enter your email address');
    }

    // now we have $email variable.
    // $email can contain space if user enter white space..
    // So lets remove any white space char using trim() function.

    $email trim($email);

    // lets connect to database.
    // These lines are cut and paste from
    // our previous signup.php
    // No need to remember, just cut and paste.

    $db_server '127.0.0.1';
    $db_user 'fwhphp_user';
    $db_password 'k5BJRaX6SFbs';
    $db_name 'fwhphp_db';

    $mysqli = new mysqli($db_server$db_user$db_password$db_name);

    if (
    $mysqli->connect_errno) {
        echo 
    'Connect failed: ' $mysqli->connect_error;
        exit();
    }

    // lets write an SQL statement to search our users table
    // to find the user having email address == $email

    $sql "select * from users where email='$email'";

    // In above line, we used single quotes between email, because it is a string.

    // Now time to execute the SQL statement.
    $result $mysqli->query($sql);

    // lets see how many records found in database
    // If num_rows is 0, no user found

    if ($result->num_rows == 0) {
        die(
    "User with email address <b>$email</b> not found");
    }

    // lets get the user record from MySQLi result

    $userInfo mysqli_fetch_assoc($result);

    // $userInfo is an associate array.
    // Lets print it.
    // Now you can see all information about the user, including password.

    echo '<pre>';
    print_r($userInfo);

    // For real life application, we will sent password to users email
    // address instead of printing it to browser.
    // But for now, this is fine for us. We fix these issues when our application is ready to go live.
    Try to understand the PHP script. Since we will be going do to PHP project in coming days, we will be doing codes like this. I tried to comment most of the lines, so you can understand what each line do.

    If you have any doubt, ask here or ask others and make sure you understand and we will be doing these type of code rest of the days.

    Exercise

    Instead of showing password on browser, make its end a mail to user. Some hints

    PHP Code:
    $name $userInfo['name'];
    $password $userInfo['password'];
    mail($email,"Your password""Hello $name, your password is $password"); 
    Hope you can do the rest.



    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  2. #2
    Join Date
    Sep 2003
    Location
    india
    Posts
    11,527

    Default

    http://php.flashwebhost.com/annie/
    lost_password.php

    PHP Code:
     <?php

    if (isset($_POST['email'])) {
        
    $email $_POST['email'];
    } else {
        die(
    'Please enter your email address');
    }

    $email trim($email);

    $db_server '127.0.0.1';
    $db_user 'fwhphp_user';
    $db_password 'k5BJRaX6SFbs';
    $db_name 'fwhphp_db';

    $mysqli = new mysqli($db_server$db_user$db_password$db_name);

    if (
    $mysqli->connect_errno) {
        echo 
    'Connect failed: ' $mysqli->connect_error;
        exit();
    }

    $sql "select * from users where email='$email'";

    // Now time to execute the SQL statement.
    $result $mysqli->query($sql);


    if (
    $result->num_rows == 0) {
        die(
    "User with email address <b>$email</b> not found");
    }

    $name $userInfo['name'];
    $password $userInfo['password'];
    mail($email,"Your password""Hello $name, your password is $password");

    echo 
    'The password has been sent to the e-mail address specified;

  3. #3
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Good, got email with password. Code working properly.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  4. #4
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    http://php.flashwebhost.com/sherly/lost_password.html
    lost_password.php


    Code:
    <?php
    
    if (isset($_POST['email'])) {
        $email = $_POST['email'];
    } else {
        die('Please enter your email address');
    }
    
    $email = trim($email);
    
    $db_server = '127.0.0.1';
    $db_user = 'fwhphp_user';
    $db_password = 'k5BJRaX6SFbs';
    $db_name = 'fwhphp_db';
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
    
    if ($mysqli->connect_errno) {
        echo 'Connect failed: ' . $mysqli->connect_error;
        exit();
    }
    
    $sql = "select * from users where email='$email'";
    
    // Now time to execute the SQL statement.
    $result = $mysqli->query($sql);
    
    
    if ($result->num_rows == 0) {
        die("User with email address <b>$email</b> not found");
    }
    
    $name = $userInfo['name'];
    $password = $userInfo['password'];
    mail($email,"Your password", "Hello $name, your password is $password");
    
    echo 'The password has been sent to the e-mail address specified;

  5. #5
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    Password not received, just got an mail with " Hello , your password is."


    http://php.flashwebhost.com/stefin/lost_password.html

    http://php.flashwebhost.com/stefin/lost_password.php



    PHP Code:

     <?php 


    if (isset($_POST['email'])) {
        
    $email $_POST['email'];
    } else {
        die(
    'Please enter your email address');
    }


    $email trim($email);


    $db_server '127.0.0.1';
    $db_user 'fwhphp_user';
    $db_password 'k5BJRaX6SFbs';
    $db_name 'fwhphp_db';


    $mysqli = new mysqli($db_server$db_user$db_password$db_name);


    if (
    $mysqli->connect_errno) {
        echo 
    'Connect failed: ' $mysqli->connect_error;
        exit();
    }


    $sql "select * from users where email='$email'";


    $result $mysqli->query($sql);




    if (
    $result->num_rows == 0) {
        die(
    "User with email address <b>$email</b> not found");
    }


    $name $userInfo['name'];
    $password $userInfo['password'];
    mail($email,"Your password""Hello $name, your password is $password");


    echo 
    'New Password send to your email';
    Last edited by stefin; 07-31-2014 at 01:55 AM.

  6. #6
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    Stefin, your program is missing 1 line.

    PHP Code:
    $userInfo mysqli_fetch_assoc($result); 
    This need to be added after code

    PHP Code:
    if ($result->num_rows == 0) {
        die(
    "User with email address <b>$email</b> not found");

    What that line do is fetch (means read, get, etc..) user information from MySQL query result.

    @annie made that mistake (but actual code worked, i verified), @sherly copied it @stefin reported the mistake :D

    @sherly

    http://php.flashwebhost.com/sherly/lost_password.html

    This is wrong code. You put code for index.html in lost_password.html

    Please reupload the code, try to understand, if you don't understand ask here, that help everyone.

    Now stefin asked the question, and we found a problem with posted codes.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  7. #7
    Join Date
    Nov 2004
    Location
    India
    Posts
    65

    Default

    Code Fixed, Mail received with password.

    PHP Code:

     <?php
    if (isset($_POST['email'])) {    $email $_POST['email'];} else {    die('Please enter your email address');}
    $email trim($email);
    $db_server '127.0.0.1';$db_user 'fwhphp_user';$db_password 'k5BJRaX6SFbs';$db_name 'fwhphp_db';
    $mysqli = new mysqli($db_server$db_user$db_password$db_name);
    if (
    $mysqli->connect_errno) {    echo 'Connect failed: ' $mysqli->connect_error;    exit();}
    $sql "select * from users where email='$email'";
    $result $mysqli->query($sql);

    if (
    $result->num_rows == 0) {    die("User with email address <b>$email</b> not found");}
    $userInfo mysqli_fetch_assoc($result);
    $name $userInfo['name'];$password $userInfo['password'];mail($email,"Your password""Hello $name, your password is $password");
    echo 
    'New Password send to your email';

  8. #8
    Join Date
    Nov 2009
    Posts
    76,596

    Default

    I got email with password

    http://php.flashwebhost.com/sherly/lost_password.html
    http://php.flashwebhost.com/sherly/lost_password.php

    Code:
    <?php
    if (isset($_POST['email'])) {    
       $email = $_POST['email'];
    } else {    
       die('Please enter your email address');
    }
    
    $email = trim($email);
    
    $db_server = '127.0.0.1';
    $db_user = 'fwhphp_user';
    $db_password = 'k5BJRaX6SFbs';
    $db_name = 'fwhphp_db';
    
    $mysqli = new mysqli($db_server, $db_user, $db_password, $db_name);
    
    if ($mysqli->connect_errno) {    
    
    echo 'Connect failed: ' . $mysqli->connect_error;    
    exit();
    
    }
    
    $sql = "select * from users where email='$email'";
    
    $result = $mysqli->query($sql);
    
    if ($result->num_rows == 0) {    
    die("User with email address <b>$email</b> not found");
    
    }
    $userInfo = mysqli_fetch_assoc($result);
    
    $name = $userInfo['name'];
    $password = $userInfo['password'];
    mail($email,"Your password", "Hello $name, your password is $password");
    echo 'The password has been sent to the e-mail address specified';

  9. #9
    Join Date
    Sep 2003
    Posts
    3,040

    Default

    @stefin, good, the code lost formatting, but it is ok, may be problem with editor.

    @sherly, nice, now it works.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •