简体   繁体   中英

PHP cookie doesn't work on WP engine server for iOS/Chrome

I set up cookie like this

function checkVisited(){
    if ( !isset($_COOKIE['visited']) ) {
        setcookie('visited', 'yes', time() + 3600*24*30, "/", "", "", TRUE);
        define('visited', false);
    }else{
        define('visited', true);
    }
}
add_action("init", "checkVisited");

Then in HTML:

<?php if(!visited): ?>
    <div class="popup">
     ...
    </div>
<?php endif; ?>

It works on Windows, but doesn't work for iOS/Chrome. And this issue appearing only on WP Engine server. I see the cookie in browser tools, but var_dump($_COOKIE['visited']) returns NULL .

By default WPEngine caches visited pages see https://wpengine.com/support/cache/

To implement a cookie dependent solution I would rely on JavaScript.

Assume that the php code you write is evaluated once and then that HTML rendered output is stored in a cache served to multiple visitors. Also keep in mind that user specific content with WPEgine's default cache will cause unexpected results.

WPEngine does support skipping the cache but it will cause extra load on their infrastructure. https://wpengine.com/support/cache/#Cache_Exclusions so it is always worth trying to respect that the cache mechanism is enabled and disabled it only when there is no other alternative.

To implement a JavaScript solution make sure that the code for the conditional cookie code is always rendered and hidden via CSS. Use JavaScript to check for cookies and show the popup only if the visited cookie is not present.

You can use pure Javascript or NPM packades to manage cookies in the browser.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM