I have a basic login / register form. When you select register it goes to my registration form ok. My problem is with the login. I want to check login information in my database and then display a different header section. I am a student and getting confused with all the different information regarding how to do this.
Currently, I am calling a javascript function on clicking login but then I want to call my php file access.php, return to my html page index.php where I want to check if the user is logged in and if so display logged_in.html.php.
login_register_form.html.php
<div id="login_form">
<form action="access.php" method="POST" name="login_form">
<label id="label_email">Email<span class ="red">*</span></label>
<label id="label_password">Password<span class ="red">*</span></label>
</br>
<input type="text" name="email" id="email"/>
<input type="password" name="password" id="password"/>
<div id="form_buttons">
<button onclick="loginUser()" Type="button" id="login" name="action" value="login">Login</button>
<button onclick="newUser()" Type="button" id="register" name="register">Register</button>
</div>
</form>
javascript
function loginUser() {
$.ajax({
url: 'access.php',
dataType: 'php'
})
window.location.assign("index.php");
}
access.php
function userIsLoggedIn()
{
if (isset($_POST['action']) and $_POST['action'] == 'login')
{
if (!isset($_POST['email']) or $_POST['email'] == '' or
!isset($_POST['password']) or $_POST['password'] == '')
{
$GLOBALS['loginError'] = 'Please fill in both fields';
return FALSE;
}
if (databaseContainsUser($_POST['email'], $password))
{
session_start();
$_SESSION['loggedIn'] = TRUE;
$_SESSION['email'] = $_POST['email'];
$_SESSION['password'] = $password;
return TRUE;
}
else
{
session_start();
unset($_SESSION['loggedIn']);
unset($_SESSION['email']);
unset($_SESSION['password']);
$GLOBALS['loginError'] =
'The specified email address or password was incorrect.';
return FALSE;
}
}
}
function databaseContainsUser($email, $password)
{
require_once('mysqli_connect.php');
$username = $password = "";
if (isset($_POST["submit"])){
$username = test_input($_POST["username"]);
$password = test_input($_POST["password"]);
}
$query = "SELECT * FROM users
WHERE username='".$_POST['username']."'
AND password = '".($_POST['password'])."'";
$result = mysqli_query($DBConnect, $query) or die();
if (mysqli_num_rows($result) > 0) {
while($row = mysqli_fetch_array($result)) {
echo "<img src={$row["avatar"]} alt='avatar image' />";
}
return TRUE;
}
else {
echo "Invalid login";
return FALSE;
}
}
and index.php
<?php
session_start();
if (!isset($_SESSION['loggedIn'])){
include 'access.php';
}
?>
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.12.0/jquery.min.js"></script>
<script type ="text/javascript" src="functions.js"></script>
</head>
<body>
<div id="body">
<div id="container">
<?php if (!isset($_SESSION['loggedIn'])){
include 'login_register_form.html.php';
}
else {
include 'logged_in.html.php';
}
?>
Any help at all would be greatly appreciated