App engine front controllers prevent cookies or headers from being created on PHP72 runtime

166 views
Skip to first unread message

Chiefo Azubike De-Lord Zuwi

unread,
Oct 18, 2020, 4:07:09 PM10/18/20
to Google App Engine
I created an issue for this problem with the link https://issuetracker.google.com/issues/166921075
My cookies are not being created and google marked this as bug for 2 months now no result. LET ME RECAP THE QUESTION HERE

ISSUE BELOW:
I am using php72 runtime on app engine. Now, after my website is up and running, I noticed that my cookies are not being created, after close inspection via gcloud logs read, I found out that header were already sent out, so I could not create or remove cookies from any page on my website only via front controller.

After multiple tests, I added setcookie on my front controller and the cookie creates successfully, but that is not what I want, I want to be able to create cookies on whatever pages I desire.

What I expected to happen:
 I expected the cookies to be created when the page loads not front controller creating my cookies for me.

Steps to reproduce:
  - Write a PHP website maybe one page
  - Host on google app engine php runtime 72 stated on app.yaml file
  - Deploy via app.yaml file.
  - Add setcookie code on website page.

Other information: ASKED QUESTIONS on stackoverflow
 
This are questions i asked on stack overflow
 https://stackoverflow.com/questions/63586479/cookie-not-storing-on-users-browser-php-7-google-app-engine-gae
 AND
 https://stackoverflow.com/questions/63616342/cannot-set-cookie-in-php-7-after-migrating-from-godaddy-to-google-app-engine-co  


This is my code recreation.

app.yaml file

     runtime: php72
     runtime_config:
     document_root:
 
     handlers:
     
    - url: /.*
       script: auto
       secure: always
       redirect_http_response_code: 301
 
    entrypoint:
       serve worker.php


worker.php //THIS WORKER.PHP below is the only place that allows the creation of cookie for me, I do  not want to be able to create cookie here only. I want it on any page I wish to create it on.
   
          <?php
          ini_set('allow_url_fopen',1);
[IF I INCLUDE cookiesetter.php here it works perfectly]
         $path = @parse_url($_SERVER['REQUEST_URI'])['path'];
         switch ($path){
         case '/':
            require 'index.php';
            break;
         case '/index':
            require 'index.php';
            break;
         case '/index.php':
            require 'index.php';
            break;
         case '/about':
            require 'about.php';
            break;
         case '/about.php':
            require 'about.php';
            break;
         case '/contact':
            require 'contact.php';
            break;
         case '/contact.php':
            require 'contact.php';
            break;
        case '/privacy':
            require 'privacy.php';
            break;
        case '/privacy.php':
            require 'privacy.php';
            break;
        case '/product-listing/':
            require __DIR__.'/product-listing.php';
            break;
        case   (preg_match('/product-listing.*/', $path) ? true : false) :
            require __DIR__.'/product-listing.php';
            break;
        case '/404':
            require '404.php';
            break;
        case '/404.php':
            require '404.php';
            break;
        default:
            http_response_code(404);
            header("Location: https://www.sharyor.com/404");
            exit();
        }
        ?>


code i use to setcookie on my site it is saved o cookiesetter.php file and included on every page.
        <?php
        if (isset($_COOKIE['sharyor_tra'])){
        } else {
        setcookie('sharyor_tra', $transactionid, time() + (3000 * 24 * 60 * 60), "/");
        @$trackingcodecookie = $_COOKIE['sharyor_tra'];  
        }
         ?>


index.php
        <html>
        <head>
        <title>Home page</title>
        <?php
         include "cookiesetter.php"; //refer to the code above where I said this code is used to create cookie referenced on cookiesetter.php
         ?>
        </head>
         <body>
         <h1>Home page</h1>
         </body>
         </html>

about.php
        <html>
        <head>
        <title>About page</title>
        <?php
         include "cookiesetter.php"; //refer to the code above where I said this code is used to create cookie referenced on cookiesetter.php
         ?>
        </head>
         <body>
         <h1>About page</h1>
         </body>
         </html>


privacy.php
        <html>
        <head>
        <title>Privacy page</title>
        <?php
         include "cookiesetter.php"; //refer to the code above where I said this code is used to create cookie referenced on cookiesetter.php
         ?>
        </head>
         <body>
         <h1>Privacy page</h1>
         </body>
         </html>


contact.php
        <html>
        <head>
        <title>Contact page</title>
        <?php
         include "cookiesetter.php"; //refer to the code above where I said this code is used to create cookie referenced on cookiesetter.php
         ?>
        </head>
         <body>
         <h1>Contact page</h1>
         </body>
         </html>

product-listing.php     //I USE REQUEST URI in product listing because GET parameter is not passing I asked a question on stack overflow too
        <html>
        <head>
        <title>Product listing page</title>
        <?php
         include "cookiesetter.php"; //refer to the code above where I said this code is used to create cookie referenced on cookiesetter.php
         ?>
        </head>
         <body>
         <h1>Product listing page</h1>
         <p>
         echo $hi = $_SERVER["REQUEST_URI"];
         </p>
         </body>
         </html>


404.php
        <html>
        <head>
        <title>404 error page</title>
        <?php
         include "cookiesetter.php"; //refer to the code above where I said this code is used to create cookie referenced on cookiesetter.php
         ?>
        </head>
         <body>
         <h1>404 error page</h1>
         </body>
         </html>



Carl Smith

unread,
Oct 18, 2020, 5:50:04 PM10/18/20
to google-a...@googlegroups.com
Setting a cookie needs to happen before any content is output, so cookiesetter.php needs to be included before the <html> tag.

Hope this helps

--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/cced8183-e9bc-4f18-89c6-08e643afbc02n%40googlegroups.com.
Reply all
Reply to author
Forward
0 new messages