I'm learning PHP and I have made a simple login script but the problem is that it only redirects me to a blank page. It's meant to redirect to index.php if user credentials are correct but this is apparently not the case? There are also validation so that if the user enters blank, an error is returned. This doesn't appear to have been executed.
login.php
<form id="login-form" method="post" action="logininc.php"> <fieldset>
<legend>Login </legend>
<p>Please enter your username and password to access the administrator's panel</p>
<label for="user"> <input type="text" name="user" placeholder="Type your username here" id="user" /></label>
<label for="password"> <input type="password" name="password" placeholder="Type your password here" id="password" /></label>
<label for="submit"> <input type="submit" class="btn btn-primary"name="submit" id="submit" value="Login" /> </label> </fieldset> </form>
logininc.php // my processing page
<?php
require_once("assets/configs/db_config.php");
$user=$_POST['user'];
$password=$_POST['password'];
if(isset($_POST['login']))
{
//To ensure that none of the fields are blank when submitting the form if
if($user || $password != NULL)
{
$user = stripslashes($user);
$password = stripslashes($password);
$user = mysqli_real_escape_string($user);
$password = mysqli_real_escape_string($password);
$sql="SELECT * FROM $test_db WHERE user='$user' and password='$password'";
$result=mysqli_query($sql);
$row=mysql_fetch_array($result);
if($row['user'] == $user && $row['password'] == $password)
{
session_start();
$_SESSION['user'] = $user;
$_SESSION['password'] = $password;
$_SESSION['loggedin'] = "true";
header("location:index.php");
}
else
{
print ('<div id="error">Computer says no.</div>');
}
print ('<div id="error">Enter something!</div>');
}
}
?>
index.php // success page
<?php //module to check logins
session_start();
if(!isset($_SESSION["loggedIn"])){
header("Location: login.php");
exit;
}
Echo 'Congratulations <b>'.$_SESSION['user'].'</b> you successfully logged in!!<br />
Your Password is: <b>'.$_SESSION['password'].'</b><br />
<a href="login.php">Logout</a>';
?>