Results 1 to 9 of 9

Thread: Day 18 - Login Page - Become PHP Expert in 30 days

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

    Default Day 18 - Login Page - Become PHP Expert in 30 days

    Login Page

    Today we will make login page for our application.

    Login page, ask user to enter user name (in our case, we use email as user name) and password. When user click "Login" button, data is sent to PHP program (login.php) using POST method.

    login.php will read email and password from $_POST variable, check the database for user with email address provided by user. If a user is found, we read the information of the user and check if user entered password is same as the password stored in database.

    login.html

    HTML Code:
    <!DOCTYPE html>
    <html>
    <head>
        <title>Login</title>
    </head>
    <body>
    
    <form action="login.php" method="post">
        Email:
        <input type="text" name="email">
        <br>
        Password:
        <input type="text" name="password">
        <br>
        <button type="submit" name="submit">Login</button>
    </form>
    
    </body>
    </html>
    This is simple HTML form as we used in other pages like signup.html, lost_password.html

    login.php

    PHP Code:
    <?php

    require 'includes/database.php';

    // lets read email and password from $_POST
    // array.

    $email $_POST['email'];
    $password $_POST['password'];

    // Lets remove any white space (space, tabs, newline etc..)

    $email trim($email);
    $password trim($password);

    // Lets find user with email address $email

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

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

    if (
    $result->num_rows == 0) {
        die(
    "No user with email address $email found");
    }

    // We have a result, so lets get that user details

    $userInfo mysqli_fetch_assoc($result);

    // lets check if password in database match the user entered password.

    if ($userInfo['password'] == $password) {
        echo 
    "Password Matched";
    } else {
        die(
    "Invalid password");
    }
    We get message "Password Matched" if user entered password is same as password stored in database.



    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

    I'm stuck in login.html. i entered the login details, still nothing happened.

    http://php.flashwebhost.com/annie/login.html

    Admin Can you please help..

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

    Default

    Quote Originally Posted by annie View Post
    I'm stuck in login.html. i entered the login details, still nothing happened.

    http://php.flashwebhost.com/annie/login.html

    Admin Can you please help..
    There is a spelling mistake in that page, cause it to stop working. It take me few minutes to find that. It was very small spelling mistake. Since we know it is spelling mistake and login.html file is too small, compare it with my code and fix it. It is nice you don't cut and paste, it is by making such errors and trying to fix it, we get experience and become better programmer.

    EDIT: If you make a spelling mistake in text in HTML page, it is not an issue, visitors see a page with spelling error, that's all. But when you have error in HTML tag, then it may not work 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

    I got it Password Matched

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


    Code:
    <?php
    
    require 'includes/database.php';
    
    $email = $_POST['email'];
    $password = $_POST['password'];
    
    $email = trim($email);
    $password = trim($password);
    
    $sql = "select * from users where email='$email'";
    
    $result = $mysqli->query($sql);
    
    if ($result->num_rows == 0) {
        die("No user with email address $email found");
    }
    
    $userInfo = mysqli_fetch_assoc($result);
    
    if ($userInfo['password'] == $password) {
        echo "Password Matched";
    } else {
        die("Invalid password");
    } 
    
    echo 'Password Matched;
    Last edited by sherlyk; 08-04-2014 at 05:14 PM.

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

    Default

    Good, it is working perfectly.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

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

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

    Default

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

    Password Matched.


    PHP Code:

    <?php 


    require 'includes/database.php'


    $email $_POST['email']; 
    $password $_POST['password']; 


    $email trim($email); 
    $password trim($password); 


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


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


    if (
    $result->num_rows == 0) { 
        die(
    "No user with email address $email found"); 



    $userInfo mysqli_fetch_assoc($result); 
     
    if (
    $userInfo['password'] == $password) { 
        echo 
    "Password Matched"
    } else { 
        die(
    "Invalid password"); 
    }
    Last edited by stefin; 08-05-2014 at 11:13 AM.

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

    Default

    Nice, most sites will redirect to another page after login instead of showing password matched. We will do this on Day 19.
    Become PHP Expert in 30 days
    FreeMarriage.com - Free Online Matrimonial
    FlashWebHost.com - Professional Web Hosting, Designing.

  9. #9
    Join Date
    Nov 2009
    Location
    kerala
    Posts
    19,076

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
  •