I have a login page I created using php, and I'm having an issue getting it to take me to the admin page after I've logged in correctly. I know the login I'm using is correct, since I just created it, and if it's wrong, it's supposed to tell me. However, once I login, the whole page just goes blank and the web address says I'm still on the login page instead of the admin one.
I checked the error log and it says "PHP Warning: Cannot modify header information - headers already sent by (output started at /admin/login.php:6) in /admin/includes/functions.php on line 4"
This is the code the error log is referring to:
function redirect_to($new_location) {
header("Location: " . $new_location);
exit;
}
I don't understand what the error is pointing out. I've used this exact code before when I was learning how to create a login and admin pages, and I didn't have any problems when I tested it out then using WAMP on my computer. Now that I've put it on my website's server, this error is popping up. I'm not sure where to begin troubleshooting because if I take out that function, then it won't redirect at all. Any help would be super appreciated!!
Here is the main code I'm using for the login page, if that helps:
<?php require_once("includes/session.php"); ?>
<?php require_once("includes/db_connection.php"); ?>
<?php require_once("includes/functions.php"); ?>
<?php require_once("includes/validation_functions.php"); ?>
<?php
$username = "";
if (isset($_POST['submit'])) {
// Process the form
// validations
$required_fields = array("username", "password");
validate_presences($required_fields);
if (empty($errors)) {
// Attempt Login
$username = $_POST["username"];
$password = $_POST["password"];
$found_admin = attempt_login($username, $password);
if ($found_admin) {
// Success
// Mark user as logged in
$_SESSION["admin_id"] = $found_admin["id"];
$_SESSION["username"] = $found_admin["username"];
redirect_to("http://thehummingbirdplace.com/admin/admin.php");
} else {
// Failure
$_SESSION["message"] = "Username/password not found.";
}
}
} else {
// This is probably a GET request
} // end: if (isset($_POST['submit']))
?>
<?php $layout_contect = "admin"; ?>
<?php include("includes/layouts/header.php"); ?>
<div id="main">
<div id="navigation">
</div>
<div id="page">
<?php echo message(); ?>
<?php echo form_errors($errors); ?>
<h2>Login</h2>
<form action="login.php" method="post">
<p>Username:
<input type="text" name="username" value="<?php echo htmlentities($username); ?>" />
</p>
<p>Password:
<input type="password" name="password" value="" />
</p>
<input type="submit" name="submit" value="Submit" />
</form>
</div>
</div>
<?php include("includes/layouts/footer.php"); ?>