I have a code and there is a problem. It's a login form that checks the user and pass and if it was ok it will create a session. In another page, it will check if the session login is the opposite of true or not. If it was true it will login. The problem is when the user wants to login, s/he needs to submit the login form 2 times so s/he can enter to site. What is the problem?
<form method="post" attribute="post" action="test.php">
<p>Username<br/>
<input type="text" id="user" name="user" class="form-control" required></p>
<p>Password<br/>
<input type="text" id="pass" name="pass" class="form-control" required></p>
<p></p>
<button type="submit" name="sub2" id="sub" value="sub" class="btn btn-default btn-block">Login</button>
</form>
<?PHP
include 'config.php';
$user = $_POST["user"];
$pass=$_POST["pass"];
if (isset($_POST['user']) and isset($_POST['pass']))
{
$conn = new mysqli($servername, $username, $password, $dbname);
$result = $link->query("SELECT user FROM users2 WHERE user = '$user'");
$userpass = $link->query("SELECT pass FROM users2 WHERE user = '$user'");
$row = $userpass->fetch_assoc();
$userpasss = $row["pass"];
if($result->num_rows == 1 and $pass == $userpasss)
{
session_start();
$_SESSION["login"] = true;
$_SESSION["username"] = "$user";
echo "hello";
}
}
?>
<?PHP
session_start();
echo $_SESSION["login"];
echo $_SESSION["username"];
?>
Any help appreciated. Thank you warm regards