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). I don't want the user to be able to access the second page without logging in, but I'm having trouble getting the code to work.
login.php page code:
<?php
// 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))
{
if (!$_SESSION)
{
session_start();
}
$_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
if ($_SESSION['logged_in'] !== "yes")
{
header("Location: http://www.mydomain.com/login.php");
die;
}
?>
successful