How can I modify this to accompany multiple users with the ability to redirect people to different ‘main.php’ pages based on their user and pass? I’d like to do this within the PHP and not use a database.
login.php
[code=php]
<?php
// start the session
session_start();
$errorMessage = ”;
if (isset($_POST[‘txtUserId’]) && isset($_POST[‘txtPassword’])) {
// check if the username and password combination is correct
if ($_POST[‘txtUserId’] === ‘theuser’ && $_POST[‘txtPassword’] === ‘thepass’) {
// the username and password match,
// set the session
$_SESSION[‘basic_is_logged_in’] = true;
// after login we move to the main page
header(‘Location: main.php’);
exit;
} else {
$errorMessage = ‘Sorry, wrong username / password’;
}
}
?>
<html>
<head>
<title>Basic Login</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<body>
<?php
if ($errorMessage != ”) {
?>
<p align=”center”><strong><font color=”#990000″><?php echo $errorMessage; ?></font></strong></p>
<?php
}
?>
<form action=”” method=”post” name=”frmLogin” id=”frmLogin”>
<table width=”400″ border=”1″ align=”center” cellpadding=”2″ cellspacing=”2″>
<tr>
<td width=”150″>User Id</td>
<td><input name=”txtUserId” type=”text” id=”txtUserId”></td>
</tr>
<tr>
<td width=”150″>Password</td>
<td><input name=”txtPassword” type=”password” id=”txtPassword”></td>
</tr>
<tr>
<td width=”150″> </td>
<td><input name=”btnLogin” type=”submit” id=”btnLogin” value=”Login”></td>
</tr>
</table>
</form>
</body>
</html>
main.php
[code=php]
<?php
// start the session
session_start();
// is the one accessing this page logged in or not?
if (!isset($_SESSION[‘basic_is_logged_in’]) || $_SESSION[‘basic_is_logged_in’] !== true) {
// not logged in, move to login page
header(‘Location: login.php’);
exit;
}
?>
<html>
<head>
<title>Main User Page</title>
<meta http-equiv=”Content-Type” content=”text/html; charset=iso-8859-1″>
</head>
<body>
<p>This is the main application page. You are free to play around here since you are an autenthicated user π </p>
<p> </p>
<p><a href=”logout.php”>Logout</a></p>
</body>
</html>
logout.php
[code=php]
<?php
// start the session
session_start();
// if the user is logged in, unset the session
if (isset($_SESSION[‘basic_is_logged_in’])) {
unset($_SESSION[‘basic_is_logged_in’]);
}
// now that the user is logged out,
// go to login page
header(‘Location: login.php’);
?>