Hi,
I'm new to PHP and even after looking at other similar threads on this forum, I am unable to correct my code.
Can anyone help?
<?php
// This is the login page for the site.
// Set the page title and include the PHP header.
$page_title = 'Login';
include ('./includes/header.html');
if (isset($_POST['submitted'])) { // Check if the form has been submitted.
require_once ('mysql_connect.php'); // Connect to the database.
// Validate the email address.
if (!empty($_POST['email'])) {
$email = escape_data($_POST['email']);
} else {
echo '<p><font color="red" size="+1">You need to enter your email address</font></p>';
$email = FALSE;
}
// Validate the password.
if (!empty($_POST['pass'])) {
$pass = escape_data($_POST['pass']);
} else {
$pass = FALSE;
echo '<p><font color="red" size="+1">You need to enter your password</font></p>';
}
if ($email && $pass) { // If everything's OK.
// Query the database.
$query = "SELECT customer_id, first_name FROM customers WHERE (email_address='$email' AND password='$pass')";
$result = mysqli_query ($dbc, $query);
if (mysqli_num_rows($result) == 1) { // A match was made.
// Register the values & redirect.
while ($row = mysqli_fetch_array ($result, MYSQLI_NUM));
mysqli_free_result($result);
mysqli_close($dbc); // Close the database connection.
$_SESSION['first_name'] = $row[1];
$_SESSION['customer_id'] = $row[0];
// Start defining the URL.
$url = 'http://' . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']);
// Check for a trailing slash.
if ((substr($url, -1) == '/') OR (substr($url, -1) == '\\') ) {
$url = substr ($url, 0, -1); // Chop off the slash.
}
// Add the page.
$url .= '/index.php';
ob_end_clean(); // Delete the buffer.
header("Location: $url");
exit(); // Quit the script.
} else { // No match was made.
echo '<p><font color="red" size="+1">The email address and password entered do not match those which you registered with.</font></p>';
}
} else { // If everything wasn't OK.
echo '<p><font color="red" size="+1">Please try again.</font></p>';
}
mysqli_close($dbc); // Close the database connection.
} // End of SUBMIT conditional.
?>
<h1>Login</h1>
<p>Your Browser Must Allow Cookies In Order To Log In.</p>
<form action="login.php" method="post">
<fieldset>
<p><b>Email Address:</b> <input type="text" name="email" size="20" maxlength="40" value="
<?php if (isset($_POST['email'])) echo $_POST['email']; ?>" /></p>
<p><b>Password:</b> <input type="password" name="pass" size="20" maxlength="20" /></p>
<div align="center"><input type="submit" name="submit" value="Login" /></div>
<input type="hidden" name="submitted" value="TRUE" />
</fieldset>
</form>
<?php // Include the PHP footer.
include ('./includes/footer.php');
?>
Any help would be greatly appreciated. Thanks in advance.