When I open the login page for the first time, even if I use correct username and password, the login page is not being redirected to home page. But on the second login it is properly being redirected to home page. The code of login.php looks like this.
<?php
session_start();
$_SESSION['login_user'] = false;
?>
Rest of login.php is simple html and the login form data is sent to validate.php where validate.php looks like this.
<?php
session_start();
$username="";
$password="";
$usernameErr="";
$passwordErr="";
$empty=false;
$_SESSION['login_user'] = false;
if(isset($_POST['submit']))
{
if(empty($_POST['username']))
{
$empty=true;
$usernameErr="Username is empty. Please try again";
}
else
{
$username=$_POST['username'];
}
if(empty($_POST['password']))
{
$empty=true;
$passwordErr="Password is empty. Please try again";
}
else
{
$password=$_POST['password'];
}
if(!$empty)
{
$servername="myxor.in.mysql";
$dbusername="myxor_in";
$dbpassword="srikanth177";
$dbname="myxor_in";
$conn=new mysqli($servername, $dbusername, $dbpassword, $dbname);
if($conn->connect_error)
{
die("Connection failed: " . $conn->connect_error);
}
$sql = "SELECT firstname,package,username,password FROM users WHERE username='$username'";
$result = $conn->query($sql);
if (($result->num_rows) > 0)
{
$row = $result->fetch_assoc();
if($username === $row['username'])
{
if($password === $row['password'])
{
$_SESSION['fname'] = $row['firstname'];
$_SESSION['pack'] = $row['package'];
//$_SESSION['login_user'] = $username;
$_SESSION['login_user'] = true;
header('Location: http://www.myxor.in/home.php');
exit;
}
else
{
$passwordErr = "Invalid Password. Please Try Again";
}
}
} else
{
$usernameErr = "Invalid Username. Please Try Again";
}
}
}
else echo "submit not set";
echo $usernameErr;
echo "<br>";
echo $passwordErr;
?>
Any suggestions why the login page is not functioning for the first time (Note that when I login second time its being properly redirected to required home.php)