Click here to Skip to main content
15,886,799 members
Please Sign up or sign in to vote.
5.00/5 (1 vote)
See more:
I'm trying to get my login system to work I followed a tutorial and made a login function but it doesnt seem to work. I got the info to store in the database to I'm just trying to allow the user to login with their data any help would be appreciated if not all good I figured I'd try to get some help I've been watching some videos but cant seem to get it to work:


Register form code to allow the user to make account: (I got all this to store in a datbase)
PHP
<?php

session_start();

include("db.form.php");
include("functions.php");

?>

<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <link rel="stylesheet" href="style.css" />
    <title>Registration Form</title>
  </head>
  <body class="registerbody">
    <img class="logoregister" src="images/logo.png" />
    <div class="registerformcontainer">
      <div class="titlestyle">Registration Form</div>
      <form action="signup.form.php" method="POST">
        <div class="inputform">
          <div class="userinputboxes">
            First Name:
            <input name="Fname" type="text" placeholder="Enter your First Name " required />
          </div>
          <div class="userinputboxes">
            Last Name:
            <input name="Lname" type="text" placeholder="Enter your Last Name " required />
          </div>
          <div class="userinputboxes">
            Enter User Name:
            <input name="USERname" type="text" placeholder="Enter your desired User name " required />
          </div>
          <div class="userinputboxes">
            Email:
            <input name="Email"
              value=""
              onchange="try{setCustomValidity('')}catch(e){}"
              type="email"
              placeholder="Enter your Email address "
              id="email"
              name="email"
              pattern="[a-z0-9._%+-]+@[a-z0-9.-]+\.[a-z]{2,}$"
              required
            />
          </div>
          <div class="userinputboxes">
            Phone Number:
            <input name="Pnum"
              type="text"
              placeholder="Enter your Phone Number "
              required
            />
          </div>
          <div class="userinputboxes">
            Date of Birth:
            <input name="Bdate" type="date" required />
          </div>
          <div class="userinputboxes">
            Password:
            <input
            name="Pass"
              type="password"
              placeholder="Enter your Password "
              required
            />
          </div>
          <div class="userinputboxes">
            Confirm Password:
            <input
            name="Passrepeat"
              type="password"
              placeholder="Confirm your Password"
              required
            />
          </div>
        </div>
        <div class="button">
          <input type="submit" value="Create account" />
        </div>
      </form>
    </div>
</html>


signup.form.php code below:

PHP
<?php

session_start();

include("db.form.php");
include("functions.php");


if($_SERVER['REQUEST_METHOD'] == "POST"){
  $usersFName = $_POST['Fname'];
  $usersLName = $_POST['Lname'];
  $usersUSERSNAME = $_POST['USERname'];
  $usersEMAIL = $_POST['Email'];
  $usersPNUM = $_POST['Pnum'];
  $usersBDATE = $_POST['Bdate'];
  $usersPWD = $_POST['Pass'];
  $Passrepeat = $_POST['Passrepeat'];


  if (!empty($usersUSERSNAME) && !empty($usersPWD) && !is_numeric($usersUSERNAME)){
   
    $query = "insert into users (usersFName, usersLName, usersEMAIL, usersUSERSNAME,usersPNUM, usersBDATE, usersPWD) values ('$usersFName', '$usersLName', '$usersEMAIL', '$usersUSERSNAME','$usersPNUM', '$usersBDATE', '$usersPWD')";
    mysqli_query($conn, $query);

    header("Location: loginpage.php");
    die;
  }
}


functions.php code below:
PHP
<?php

function check_login ($conn){
  if(isset($_SESSION['usersUSERSNAME'])){
     $id = $_SESSION['usersUSERSNAME'];
     $query = "select * from users where usersUSERSNAME = '$id' limit 1";

     $result = mysqli_query($conn, $query);
     
     if($result && mysqli_num_rows($result) > 0){

      $user_data = mysqli_fetch_assoc($result);
      return $user_data;
     }
  }

  //go back to login
  header("Location: loginpage.php");
  die;
}


loginpage.php code below: Trying to allow the user to enter their account they made here to have access to main homepage
PHP
<?php

session_start();

include("db.form.php");
include("functions.php");

?>



<!DOCTYPE html>
<html>
  <head>
    <meta charset="utf-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>L&B Furniture</title>
    <link rel="stylesheet" href="style.css" />
    <link rel="preconnect" href="https://fonts.googleapis.com" />
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
    <link
      href="https://fonts.googleapis.com/css2?family=Poppins&display=swap"
      rel="stylesheet"
    />
  </head>
  <body onload="slider()">
    <div class="back">
      <div class="slider">
        <img src="images/bedroom.jpeg" alt="" id="slideImg" />
      </div>
      <div class="overlay">
        <div class="nav-design">
          <div class="logo">L&B FURNITURE</div>
        </div>
        <div class="content">
          <h1>L&B is all you need!</h1>
          <h3>
            All customers are required to create an account before they are able
            to continue to the homepage
          </h3>
          <h3>Do you have an account already? Please login in below:</h3>
          <div>
            <form action="login.form.php" method="POST">
              <label class="usernamestyle">User Name :</label
              ><input
                class="loginbox"
                style="margin-bottom: 100px"
                type="text"
                name="UserName"
                placeholder="Enter User Name"
              /><br />
              <label class="passwordusername">Password: </label
              ><input
                class="passwordbox"
                type="password"
                name="password"
                placeholder="Enter Password"
              />
              <button class="loginbuttons" type="submit">Login</button>
            </form>
            <form action="register.form.php" method="POST">
              <button class="registerbutton" type="Submit">
                Create an account
              </button>
            </form>
          </div>
        </div>
      </div>
    </div>
    <script>
      var slideImg = document.getElementById("slideImg");

      var images = new Array(
        "images/bedroom.jpg",
        "images/office.jpg",
        "images/livingroom.jpg",
        "images/diningroom.jpg",
        "images/bathroom.jpg"
      );

      var len = images.length;
      var i = 0;

      function slider() {
        if (i > len - 1) {
          i = 0;
        }
        slideImg.src = images[i];
        i++;
        setTimeout("slider()", 4000);
      }
    </script>
  </body>
</html>


Lastly my login.form.php which I linked to my loginpage.php:

PHP
<?php

session_start();

include("db.form.php");
include("functions.php");

if($_SERVER['REQUEST_METHOD'] == "POST"){
  $usersFName = $_POST['Fname'];
  $usersLName = $_POST['Lname'];
  $usersUSERSNAME = $_POST['USERname'];
  $usersEMAIL = $_POST['Email'];
  $usersPNUM = $_POST['Pnum'];
  $usersBDATE = $_POST['Bdate'];
  $usersPWD = $_POST['Pass'];
  $Passrepeat = $_POST['Passrepeat'];


  if (!empty($usersUSERSNAME) && !empty($usersPWD) && !is_numeric($usersUSERNAME)){
   //read from database
    $query = "select * from users where usersUSERSNAME = '$USERname' limit 1";
    $result = mysqli_query($conn, $query);

    if($result){
        if($result && mysqli_num_rows($result) > 0){

            $user_data = mysqli_fetch_assoc($result);
            
            if($user_data['Pass'] === $usersPWD){
                $_SESSION['USERname'] = $user_data['usersUSERSNAME'];
                header("Location: homepage.php");
                die;
            }
           }
    }

    
  }
}


What I have tried:

I tried not making a separate login.form.php and putting all the code in loginpage.php but that didn't work.
Posted
Updated 24-Nov-21 11:44am

1 solution

Wherever you got the tutorial, ignore it or bin it. That code is dangerous! Never concatenate strings to build a SQL command. It leaves you wide open to accidental or deliberate SQL Injection attack which can destroy your entire database. Always use Parameterized queries instead.

When you concatenate strings, you cause problems because SQL receives commands like:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'Baker's Wood'
The quote the user added terminates the string as far as SQL is concerned and you get problems. But it could be worse. If I come along and type this instead: "x';DROP TABLE MyTable;--" Then SQL receives a very different command:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';DROP TABLE MyTable;--'
Which SQL sees as three separate commands:
SQL
SELECT * FROM MyTable WHERE StreetAddress = 'x';
A perfectly valid SELECT
SQL
DROP TABLE MyTable;
A perfectly valid "delete the table" command
SQL
--'
And everything else is a comment.
So it does: selects any matching rows, deletes the table from the DB, and ignores anything else.

So ALWAYS use parameterized queries! Or be prepared to restore your DB from backup frequently. You do take backups regularly, don't you?

Then there is your passwords ... Never store passwords in clear text - it is a major security risk. There is some information on how to do it here: Password Storage: How to do it.[^]

And remember: this is web based so if you have any European Union users then GDPR applies and that means you need to handle passwords as sensitive data and store them in a safe and secure manner. Text is neither of those and the fines can be .... um ... outstanding. In December 2018 a German company received a relatively low fine of €20,000 for just that.

See what I mean? whoever wrote that code clearly had no idea what the heck he was doing - so the only thing you could possible learn form it is "how not to do it". The chances are that code has never worked properly, and the "author" had as much idea as you do how to write code ... bin it.
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900