PHP login system request with backend side

  authentication, php

I know this might be much to ask to ask, but I have some HTML code, and a working signup system and a working connection to my database called $connection. Now, it’s time for the login system. Now, I’ve tried various login tutorials and referred to various login tutorials, but still haven’t found any which ACTUALLY work. What I’m trying to do is, on my login page, it’s where my login fields are and stuff, but the actual backend code is on my connection.php file. I’ve tried this code, but it doesn’t work:

$loginEmail = $_POST['uname'];
$loginPassword = $_POST['psw'];

if (isset($_POST['loginbutton'])) {
  if(!empty($loginEmail) && !empty($loginPassword)) {
    // read from database
    $query = "SELECT * FROM users WHERE email = '$loginEmail'";

    $res = mysqli_query($connection, $query);

    if($res)
    {
      if($res && mysqli_num_rows($res) > 0)
      {
        $user_data = mysqli_fetch_assoc($result);

        if($user_data['password'] === $hashed_password)
        {
          $_SESSION['uname'] = $user_data['uname'];
          header("location: home.php");
          die;
        } else {
          echo "Please enter some valid information";
        }
      } else {
        echo "Please enter some valid information";
      }
    }
  } else
  {
    echo "Please enter some valid information!";
  }
}

So, whatever error occurs, it should show up on my connection.php page, on the click of the login button, in this case "Please enter some valid information", but in this case it isn’t so something is wrong. So could you please maybe build a login system for me. Like just the PHP code. Here are the email and password variables:

$loginEmail = $_POST['uname'];
$loginPassword = $_POST['psw'];

HTML code:

<form class="modal-content animate" action="connection.php" method="post">
    <div class="imgcontainer">
      <span onclick="location.href='index.php'" class="close" title="Close Modal"></span>
      <img src="https://cdn4.iconfinder.com/data/icons/small-n-flat/24/user-alt-512.png" alt="Avatar" class="avatar">
    </div>

    <div class="container">
      <label for="username"><b></b></label>
      <i style="color: green; margin-top:25px; margin-left:393px; position:absolute; visibility: hidden" class="fa fa-check-circle" id="emailYes"></i>
      <i style="color: red; margin-top:25px; margin-left:393px; position:absolute; visibility: hidden" class="fa fa-exclamation-circle" id="emailNo"></i>
      <input class="ema" id="ema" type="text" placeholder="Email Address" name="uname">
      <i style="color: green; margin-top:25px; margin-left:393px; position:absolute; visibility: hidden" class="fa fa-check-circle" id="passwordYes"></i>
      <i style="color: red; margin-top:25px; margin-left:393px; position:absolute; visibility: hidden" class="fa fa-exclamation-circle" id="passwordNo"></i>

      <label for="psw"><b></b></label>
      <input class="pass" id="pswd" type="password" placeholder="Password" name="psw">

      <button type="submit" id="loginBtn" class="ltn" style="border-radius:10px; outline:none; font-family:inherit; color:white; font-size:17px" name="loginbutton"><b>Log In</b></button>
      <label>
        <small style="position:fixed; margin-top: 62px; font-size:20px; margin-left:-377px; color:red; visibility:hidden" id="loginError"></small>
        <small style="position:fixed; margin-top: 62px; font-size:20px; margin-left:-321px; color:red; visibility:hidden" id="emailError">Please enter your email.</small>
        <small style="position:fixed; margin-top: 62px; font-size:20px; margin-left:-337px; color:red; visibility:hidden" id="passError">Please enter your password.</small>
    </div>

<div class="container">
  <hr style="margin-top:-10px">
</div>

    <div class="container">
      <center><button type="button" class="signupbtn" id="sgnbtn" style="border-radius:10px; margin-top:-50px; font-family:inherit; color:white; font-size:15px; margin-left: -210px"><b>Sign Up</b></button></center>
      <center><button type="button" class="forgotPass" id="forgotPass" style="border-radius:10px; margin-top:-47px; position:fixed; font-family:inherit; color:white; font-size:15px; margin-left:20px"><b>Forgot Password?</b></button></center>
    </div>
  </form>
</div>

But something is happening. On the connection page, it’s showing something. Like when I enter the fields for my login page, in my connection page, it shows nothing. But when the click the login button, and refresh in my connection.php page, it says "Email already exists". But that’s related to my signup script, I wrote that PHP for my signup system, I don’t know why it’s showing it when I enter the login fields and click on the login button. It should only occur when, in my signup fields, the email entered already exists, but it’s occurring when I fill the login fields.

So, if possible, with those two email and password variables provided, could you possibly build me a working login system. Thank you so much! 🙂

Source: Ask PHP

LEAVE A COMMENT