How to fix “Headers already sent” error in PHP does not solve the issue because I'm not getting any headers set error. The cookie is being set, the problem is the link going to main page ('/'). It does not seem to go to the main page anymore and even on the trace logs, I do not see entries that spash.php is being reloaded. Its almost like after redirection from index.php (when typing http://domain), the browser/server recognizes http://domain/splash.php as the main page.
- PHP Version: 7.0.22
- Apache Version: 2.4.27
- OS: linux
I have 2 pages, index.php and spash.php. index.page checks first for authenticated cookie. If false or not found, page redirects to splash.php spash.php then sets the authenticated cookie and have anchor tag with "/" href value. But after clicking the link button, the page only reloads spash.php.
I added trace error_logs on the 2 pages, from the logs, I can see that on first load, it passes through the index.php page, then loads the spash.php page but after that even after multiple clicks on the link button, no logs that accesses the index.php page anymore.
here is my code:
index.php
<?php if(!isset($_COOKIE['authenticated'])){
error_log("This is Index, Redirect to Splash Page");
header("Location: /splash.php" );
die();
exit();
}else{
echo "meron";
}
?>
<html>
<head>
<title>Index Page</title>
</head>
<body>
<h1>Hello</h1>
<?php var_dump($_COOKIE); ?>
</body>
</html>
splash.php
<?php setcookie('authenticated', 1, 0, '/');
error_log("This is Splash, set the cookie");
echo $_SERVER['SCRIPT_FILENAME'];
?>
<html>
<head>
<title>Splash Page</title>
</head>
<body>
<a href="/">GO!</a>
</body>
</html>
NOTE:
After loading the splash page, if i type [domain]/index.php, I get to see the main page.
This issue only happens on the host, tried the code locally, everything works
If I change the href of the link button from spash.php (GO!), it is working. But I need that value to be just the domain.