Suggested articles for you:

21 thoughts on “PHP Login Form using MySQL

  1. amani

    prepare(“INSERT INTO profile(FULLNAME, EMAIL, USERNAME, PASSWORD CPASSWORD) VALUES(?, ?, ?, ?)”); //Fetching all the records with input credentials $stmt->bind_param(“ssss”, $inFullname, $inEmail, $inUsername, $encryptPassword,$encryptCPassword); //Where s indicates string type. You can use i-integer, d-double $stmt->execute(); $result = $stmt->affected_rows; $stmt -> close(); $db -> close(); if($result > 0) { header(“location: RegSuccess.php”); // user will be taken to the success page } else { echo “Oops. Something went wrong. Please try again”; ?> Try Login
    how can i resolve this problem sir…

  2. Jose Lopez

    Hi Ravi,
    Don’t know if this is happening with anyone else, but the Login.php page will not redirect to the UserProfile.php unless the HTML sections are removed. I removed the HTML tags and that did the trick. I found the fix here at
    Hope this helps anyone that ends up with a white page after submitting the LoginForm.php.
    That being said, thank you for this code.. saved me lots of work.

  3. Shreya

    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->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
    echo “Incorrect username or password”;

    1. Ravi Bandakkanavar Post author


      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.

  4. 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?

  5. 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.

      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.

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


    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.

    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, etc.

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

  7. 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.

  8. K.Sangavi

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

    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.

  9. Dinesh

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

  10. Kiran

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

      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.


Did it help? Comment here..