I had been using the header() a lot in my application to redirect to different pages, have a look at my code below.i have put the code in top of the page and i haven’t used any comments
if(isset($_SESSION['loggedin'])){
header("Location: dashboard.php");
exit;
}
else {
require_once('models/validation.php');
require_once('config/database.php');
and below on line 50 i have put on some codes to check the login credentials and redirect it to dashboard.php
if( !empty($_POST['username']) || !empty($_POST['password'])) {
if( check_login($_POST['username'], $_POST['password'])) {
header("Location: dashboard.php");
exit;
the first set of code works fine because when i login and try to access the index.php the first set of code come into action and push me back to dashboard.php
i am facing the problem with the second set of code below when i login successfully it does not redirect me to dashboard.php instead it gives me the following error
Warning: Cannot modify header
information – headers already sent by
(output started at
/home/bhatkaln/public_html/test/admin-login/index.php:17)
in
/home/bhatkaln/public_html/test/admin-login/index.php
on line 54
where i am going wrong? is it fine to use header() for redirecting or i should consider using alternative
<meta http-equiv="refresh" content="0;dashboard.php"/>";
Edit : Sorry Guys i updated my error code and here is my line 53, 54 and 55.
if( check_login($_POST['username'], $_POST['password'])) {
header("Location: dashboard.php");
exit;
Header() must be called before any other output from your PHP.