0

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:

  1. After loading the splash page, if i type [domain]/index.php, I get to see the main page.

  2. This issue only happens on the host, tried the code locally, everything works

  3. 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.

Laviel
  • 1
  • 3
  • Is the cookie set? – Andreas Aug 17 '17 at 06:41
  • yup. cookie is being set. because when I go to /index.php it works so the cookie is being read – Laviel Aug 17 '17 at 06:43
  • In splash.php is the php tag on the very first position of the file? Meaning first row and first "column"? (No indenting?) I hate autocorrect. – Andreas Aug 17 '17 at 06:43
  • yes, the php tag is on the top without indents – Laviel Aug 17 '17 at 06:53
  • Possible duplicate of [How to fix "Headers already sent" error in PHP](https://stackoverflow.com/questions/8028957/how-to-fix-headers-already-sent-error-in-php) – Jim Wright Aug 17 '17 at 07:11
  • @jim the header solutions, are they supposed to be on the index.php before redirecting or from splash.php? – Laviel Aug 17 '17 at 07:50
  • @Laviel Your redirect header isn't working because you have `echo` before it, which sends it's own headers. Make sure your errors are turned on and look at the accepted answer of the question I marked as duplicate. – Jim Wright Aug 17 '17 at 08:23
  • @Jim, I've modified the code of index.php and still the issue is there. Additionally, the "redirection" issue I'm getting is not from index.php going to spash.php BUT from splash.php going to http://domain (which is index.php) that's being accessed after clicking the link buton. – Laviel Aug 17 '17 at 08:28
  • You mean the `a` tag isn't working? – Jim Wright Aug 17 '17 at 08:33
  • add sleep(2) before redirecting and try whether its working or not. I have faced this kind of issues in my previous projects. – Rajapandian Aug 17 '17 at 08:46
  • @rajapandian, sleep from where? index or splash? – Laviel Aug 17 '17 at 08:48
  • @JimWright, yes the a tag is not working either with href="/" or href="http://mydomain.rocks" – Laviel Aug 17 '17 at 08:50
  • @Laviel after setcookie and before redirection – Rajapandian Aug 17 '17 at 08:57
  • @Rajapandian, sorry i'm getting confused. before redirection is from index and after setcookie is from splash. but anyway, I tried on both pages but still the issue is there. – Laviel Aug 17 '17 at 09:04
  • Is there such a thing as apache, php or server saving the default page after being redirected from accessing the site through a relative path? Because from what I'm seeing, there is no header issue when redirecting from index to splash, the cookies are being set and its only the anchor tag that would not point to the main page when clicked, and it would not even reach my log trace. – Laviel Aug 17 '17 at 09:11
  • this issue is weird....is there any difference after click www and without www ? – Rajapandian Aug 17 '17 at 09:43

3 Answers3

0

When you are using header location, there should not be any echo or space before.

mukund
  • 2,253
  • 1
  • 18
  • 31
0

OK I found an Indirect solution. Instead of handling the redirection from the server-side code (PHP) I used javascript and now its working.

see updated code below:

index.php

<html>
<head>
    <title>Index Page</title>
    <script>
        document.addEventListener('DOMContentLoaded', onLoad());

        function onLoad(){
            if(getCookie('astig') == ''){
                window.location.href = "/splash.php";
            }

        }

        function getCookie(cname) {
            var name = cname + "=";
            var decodedCookie = decodeURIComponent(document.cookie);
            var ca = decodedCookie.split(';');
            for(var i = 0; i <ca.length; i++) {
                var c = ca[i];
                while (c.charAt(0) == ' ') {
                    c = c.substring(1);
                }
                if (c.indexOf(name) == 0) {
                    return c.substring(name.length, c.length);
                }
            }
            return "";
        }
    </script>
</head>
<body>
    <h1>Hello</h1>
    <?php var_dump($_COOKIE); ?>
</body>

spash.php

<html>
<head>
    <title>Splash Page</title>
    <script>
        document.addEventListener('DOMContentLoaded', onLoad());

        function onLoad(){
            document.cookie = "astig=true;0;path=/";

        }

    </script>
</head>
<body>
    <a href="http://laviel.rocks">GO!</a>        
</body>

Laviel
  • 1
  • 3
0

Got the solution from the hosting provider's support. He disabled the mod expires caching. So now I can have the redirection through pure server-side script.

Just not sure what the overall effect of this option and which approach would be better.

Laviel
  • 1
  • 3