0

I am creating a cloud based php application and When I use the code below in the top of my PHP document it creates the cookie perfectly.

setcookie("lgUsr",'admin', time() + (3600 * 24 * 30), "/");

But when I put it like below as a form submission it does not create the cookie in the browser.

    <?php
        if(isset($_POST['login-submit'])){
            $usrName = $_POST['username'];
            if($usrName == 'admin'){
                    setcookie("lgUsr",'admin', time() + (3600 * 24 * 30), "/");
                ?>
                <script>
                    var resP = document.getElementById('result');
                    resP.classList.add('success-text');
                    resP.innerHTML = 'Successfully Logged In.';
                </script>
                <?php
                header("Location: admin-dashboard.php");
            } else{
                ?>
                <script>
                    var resP = document.getElementById('result');
                    resP.classList.add('error-text');
                    resP.innerHTML = 'Error Occured while logging in.';
                </script>
                <?php
            }
        }
    ?>

What could be the problem? Any kind of help is really appreciated. Both codes above works perfectly in the localhost, GAE is the one that gives the problem.

  • Have you checked the PHP error log? Have you also enabled debug logging? – hakre Oct 09 '21 at 21:38
  • `"PHP message: PHP Warning: Cannot modify header information - headers already sent by (output started at /workspace/admin.php:8) in /workspace/admin.php on line 59"` This is what it returns `line 8` is ` ` and `line 58` is where I have the `setcookie`. – Dulan Pabasara Oct 09 '21 at 21:48
  • 1
    Have you seen this: https://stackoverflow.com/a/8028987/231316 – Chris Haas Oct 09 '21 at 21:52
  • Okay, was a quick guess, (but/and) me assumed something in that direction. So what @ChrisHaas said. – hakre Oct 09 '21 at 22:02
  • @ChrisHaas Adding the form submission catch to the top of the doc did the trick for now. – Dulan Pabasara Oct 09 '21 at 22:10
  • 1
    Hi @DulanPabasara, If you managed to solve the issue, please post your solution as an answer, so other community members can upvote it if they encountered the same problem. Thank you. – Marc Anthony B Oct 11 '21 at 05:58

1 Answers1

0

Apparently, App Engine only allow once to modify the headers. So, adding the setcookie() function containing code block to the top of the refresh page does the trick. Even a space in front of <?php tag can stop the page from modifying the header. More information can be taken from this Stack Overflow answer

Adding into this, according to the setcookie PHP manual:

setcookie() defines a cookie to be sent along with the rest of the HTTP headers. Like other headers, cookies must be sent before any output from your script (this is a protocol restriction). This requires that you place calls to this function prior to any output, including <html> and <head> tags as well as any whitespace.

Marc Anthony B
  • 3,635
  • 2
  • 4
  • 19
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 11 '21 at 14:13