<?
require('connect_var.php');
require('connect.php');
$PHP_SELF=$_SERVER['PHP_SELF'];
unset($error, $_GET['error'], $_POST['error'], $_SESSION['error']);
if(!$_POST['username']){
$error .= "You must enter a username.<br>\n";
}
if(!$_POST['password']){
$error .= "You must enter a password.<br>\n";
}
if($_POST['username'] && $_POST['password']){
$login=mysql_fetch_row(mysql_query("SELECT username, password, name FROM user WHERE username='".
addslashes(trim($username))."'"));
$name=$login[2];
if($login[0] != $username){
$error .= "The username $username was not found in our database.";
}
if($login[1] != $password){
$error .= "You have entered an incorrect password for the account $username.";
}
if (!$error) {
session_start();
$_SESSION['username'] = $username; // Successfully logged in
$_SESSION['password'] = $password; // create session and
require("member_area.php"); // pass control to member_area.php.
} else {
// Not logged in
$html=<<<HEREDOC
<html>
<head>
<title>XYZ Login</title>
</head>
<body>
You could not be logged in because of the following problem(s):<br>
<font color="990000">$error</font><br>
<form method="POST" action="$PHP_SELF">
<table width=251 border=0 cellspacing=0 cellpadding=0>
<tr>
<td width=71>Login</td>
<td width=180> </td>
</tr>
<tr>
<td>Username:</td>
<td><input name="username" type="text" size="30" value="$username"></td>
</tr>
<tr>
<td>Password:</td>
<td><input name="password" type="password" size="30"></td>
</tr>
</table>
<p><input type="submit" name="Submit" value="Submit"></p>
</form>
</body>
</html>
HEREDOC;
echo $html;
?>
I changed your include() to require() because if include() fails, you don't even get an error message, your script runs anyway. I think because this is a security script you'd rather the whole script die if one of those files can't be loaded.
If they have some kind of error, then it displays the login form which points back to this php file.
If they entered a username, go ahead and fill in the form with it.
Display the error above the form. Your old script was displaying the error message before the <html> tag. Not good!
The only problem I see is that
HEREDOC makes life so much easier. You can mix and match slashes and variables (but not PHP code).
Now in all your other scripts which are member's only, just
if (!$_SESSION['username'] or !$_SESSION['password']) {
// use header() command here
}