Suggested articles for you:

18 thoughts on “PHP Login Form using MySQL

  1. Shreya

    Sir,
    I had already entered the data by mysql query, by not creating any registration file. But still to check whether the data is getting inserted or not, I created a registration file.But the same problem is happening with this too. The ‘register.php’ page has been showing as blank.
    Sir, is there any alternative option for this section?

    $stmt= $db->prepare(“SELECT USERNAME, PASSWORD FROM PROFILE WHERE USERNAME = ?”); //Fetching all the records with input credentials
    $stmt->bind_param(“s”, $inUsername); //You need to specify values to each ‘?’ explicitly while using prepared statements
    $stmt->execute();
    $stmt->bind_result($UsernameDB, $PasswordDB); // Binding i.e. mapping database results to new variables

    //Compare if the database has username and password entered by the user. Password has to be decrpted while comparing.
    if ($stmt->fetch() && password_verify($inPassword, $PasswordDB))
    {
    $_SESSION[‘username’]=$inUsername; //Storing the username value in session variable so that it can be retrieved on other pages
    header(“location: UserProfile.php”); // user will be taken to profile page
    }
    else
    {
    echo “Incorrect username or password”;
    ?>
    Login
    <?php
    }

    Reply
    1. Ravi Bandakkanavar Post author

      Shreya,

      You can replace the line
      if ($stmt->fetch() && password_verify($inPassword, $PasswordDB)) with if($stmt->fetch()) and test.
      If that works, then try if($stmt->fetch() && $inPassword=$PasswordDB).

      You can type echo "Password = " . $PasswordDB; and check what value is returned.
      —————————————————
      Your register.php is not working fine hence you are not being taken to the next level. Hope you are executing this in WAMP environment.

      Reply
  2. Shreya

    hello sir,
    my login.php page has been showing incorrect username and password even if I login with the correct username and password mentioned in the database. Can you help with this?

    Reply
  3. Nick Tate

    Hi Ravi,
    Thank you very much for your excellent tutorial. I’m running it on a Raspberry Pi 3.

    There is just one little error in the “Logout.php” file.
    The line “mysql_close($connection);” should be “mysql_close($connection);”
    because the older function was depreciated from PHP 7.

    Reply
      1. Ravi Bandakkanavar Post author

        Hi Nick,

        Thanks for pointing it out. Sorry about that.

        I corrected it. Take a look now.

        Since we are storing the database connection object in the variable $db, we should use the variable $db while closing a connection and it should be done at two places.
        1.Login.php
        2.Logout.php

        $db = mysqli_connect(DB_SERVER,DB_USERNAME,DB_PASSWORD,DB_DATABASE);
        .
        .
        mysqli_close($db);

        Reply
  4. S ALOKA PATRO

    hello sir, i need to make a website with login page.which type of validation should be best for me ?i have tried js validation but at the background in the pagesource somebody might find my id and password.please help me sir.

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Aloka,

      Yes. Using javascript would expose your values. You can overcome this by using database validation.
      I have mentioned following chunk of code in Login.php.

      if ($_SERVER['REQUEST_METHOD'] == 'POST')
      {
      if (empty($_POST['username']) || empty($_POST['password'])) //This is the way to validate inputs using PHP code but here we are using javascript validations so it is not necessary
      {
      echo "Please enter the correct Username and Password";
      //header("location: LoginForm.php");//You will be sent to Login.php for re-login
      }

      The database validation would consume little more time as compared to JS validations but in the case of JS, it would increase the web page size. You need to choose based on your requirement.

      If you still want to go for Javascript validations, you can use document.getElementById() to read from fields and have the code at a separate place by giving a reference in the head.

      My suggestion to you: go for certain frameworks which will reduce a lot of your work. some famous ones are https://laravel.com/, https://cakephp.org/ etc.

      Keep us updated with what you choose and about your website and Good Luck :)

      Reply
  5. Ravi Bandakkanavar Post author

    Updated the Login application with PHP 7.0 supported methods as many methods like mysql_real_escape_string(), mysql_query(), mysql_num_rows(), mysql_connect(), mysql_select_db(), mysql_close() were depricated in PHP 5 and removed from the latest version of PHP i.e. PHP 7.0.

    Reply
  6. K.Sangavi

    Hi Sir,
    How to run php in xamp. How to connect html and sql for online library renewal system for mini project.

    Reply
    1. Ravi Bandakkanavar Post author

      Hi Sangavi,

      Please go through following post to understand on html or php files location and also to create SQL database.
      Xamp and wamp both are almost same.

      Reply
  7. Dinesh

    can you give me an idea for my mini project so that it will be helpful for my career.

    Reply
  8. Kiran

    Good post sir. Its easy to unserstand.
    Logic to find number of logged in users is nice.
    Thank you sir.

    Reply
      1. Khabib

        I just change my local server from Apache Triad 2 to XAMPP and PHP7. I was getting crazy because what I’ve been create dont work at all. Including a simple login page. Then I found this page and this is very helpfull. I very appreciate.

        Thank You very much mate. Keep on posting.

        Reply

Did it help? Comment here..