I'm trying to create a simple PHP login, where the user logs in on the first page (login.php), and if the login is correct, is directed to the second page (page.php). The problem with my code is that the user is required to login twice in order to be successfully taken to the second page (page.php). Here's my code:
login.php page code:
<?php
session_start();
// Define your username and password
$username = "a";
$password = "b";
# define a redirect url
$redirect_url = "http://www.mydomain.com/page.php";
if (($_POST['txtUsername'] == $username) && ($_POST['txtPassword'] == $password))
{
$_SESSION['logged_in'] = "yes";
header("Location: $redirect_url");
die;
}
?>
<h2>Please Login</h2>
<form name="form" method="post" action="<?php echo $_SERVER['PHP_SELF']; ?>">
<p>
<br/>
<label for="txtUsername">Username*</label>
<br/>
<input name="txtUsername" type="text" id="txtUsername" size="30" />
<br/>
<label for="txtPassword">Password*</label>
<br/>
<input name="txtPassword" type="password" id="txtPassword" size="30" />
<br/>
<br/>
<input name="Submit" type="submit" value="Login" />
</p>
</form>
page.php page code:
<?php
session_start();
if ($_SESSION['logged_in'] !== "yes")
{
header("Location: http://www.mydomain.com/login.php");
die;
}
?>
login successful