I am creating a very simple website with user accounts. In my 'login.php', the login details are sent via a form to the page 'home.php' where the username/password are checked with a database and then:
If they are correct, the HTML in my page home.php is displayed. If they are not correct, the HTML is not displayed and I use the 'header' function to go back to the login.php page.
Here is my code :
<?php
if ($identification==1) { // IF THE IDENTIFICATION IS CORRECT
?>
// Here all the HTML of my page is displayed
<?php
}else{
header ('location : login.php');
}
?>
When I do it this way, the redirection is not made and I just have a blank page, but when I change it to:
<?php
if ($identification==1) {
?>
<!-- Here all the HTML of my page is displayed -->
<?php
}else{
?>
echo "<script type='text/javascript'>document.location.replace('login.php');</script>";
<?php
}
?>
It works perfectly! Do you have an idea why it is not working with header?
Also, is it secured to do the check of username/password directly in the home page, or should I do it in a 'test.php' page which would redirect to the 'home.php' page if the credentials are correct ?
EDIT, works like this
if ($identification!=1) { header ('Location: home.php'); exit;
};