I'm a beginner at php and I have created a simple login system with php and mysqli for my website however my code wont validate such as "username doesnt exist" and im not sure how to make the account for the user so that it is individual. My code is in 3 files and all linked. connection.php contains session_star(); and connection to the database Homepage.php - what the form appears on
<?php
include 'login.php';
?>
<section>
<img id="image1" src="homepic.jpg" alt="logo" />
</section>
loginform.php
<link rel="stylesheet" href="homecss.css"/>
<div id="form">
<h2>Login</h2>
<form method="post" action="loginSubmit.php">
User Name: <br/>
<input type="name" name="username" type="text" /><br />
Password: <br/>
<input id="password" name="password" type="password" /><br />
<input type="submit" name="logsubmit" value="Login" />
</form></div>
login.php
<link rel="stylesheet" href="homecss.css"/>
<?php
include './connection.php';
?>
<div id="form">
<?php
if(!isset($_SESSION['authenticatedusername'])){
include './loginform.php';
if (!empty($_POST['username'])) {
echo "Username is required";
}
if (!empty($_POST['password'])) {
echo "Password is required";
}
}
else{
echo 'welcome '. $_SESSION['authenticatedusername'];
echo '<br/><a href ="logout.php"> logout </a>';
echo '<br/><a href ="account.php"> My account </a>';
//check to see if error message is to be displayed
if (isset($_SESSION['message'])){
echo $_SESSION['message']="login failed";
}
?>
</div>
loginSubmit.php
<?php
include "connection.php";
if(isset($_POST['logsubmit'])){
$user=$_POST['username'];
$pass=$_POST['password'];
$query= "SELECT * FROM users WHERE user_name='$user' AND user_password='$pass'";
$result=mysqli_query($connection, $query);
if ($row = mysqli_num_rows($result) >0){
$_SESSION['authenticatedusername']=$user;
header ("Location: homepage.php");
} else{
echo $_SESSION['message'];
header("Location: homepage.php");
}
}
?>