Using PHP 7.1, MySQL, HTML5 Using localhost at present, I wanted to set-up a redirect from each webpage if the user is not logged in, to return to the login page login.php.
So I added the following include header.php to all of my PHP files
<!-- header.php
on all webpages, checks if user logged in, redirects to login.php if NOT
https://stackoverflow.com/questions/29202153/how-to-redirect-users-to-login-page-if-they-havent-logged-in
-->
<?php
session_start();
if(empty($_SESSION["username"])){ /* IF NO USERNAME REGISTERED TO THE SESSION VARIABLE */
header("LOCATION:login.php"); /* REDIRECT USER TO LOGIN PAGE */
}
?>
I am now getting the error
localhost redirected you too many times.
Having cleared all my cookies as recommended and rebooted my system, and I have removed the call to header.php from about 40 php files, it is still a problem.
I should say that it worked 100% until I edited my approximately 40th PHP file to add
<?php require('header.php'); ?>
Then the error was displayed in the chrome browser as follows.
This page isn’t working
localhost redirected you too many times.
Try clearing your cookies.
ERR_TOO_MANY_REDIRECTS
I can add the header.php to less files in the future i.e. by adding to a higher level php file.
- how do I fix the error so I can continue to develop and
- what change do I make to the code to prevent the error in the future.
I assume the system is now in an infinite loop, which needs to be cleared
I am desperate for a quick solution so any help would be much appreciated, I will continue looking for a solution in the meantime.
Many Thanks in advance, Colin