WordPress.org

Ready to get started?Download WordPress

Forums

Setcookie/php-redirection conflicting with W3 Total Cache (4 posts)

  1. SgrWd
    Member
    Posted 2 years ago #

    I have a simple setcookie and redirect code that works fine for logged in users (who aren't cached) but it doesn't work for non-logged in users (who are cached). W3 Total Cache is conflicting with this code, and even when I tell W3 Total cache not to cache header.php it still doesn't work.

    <?php
    if(is_front_page() )
    {
    if( !(isset($_COOKIE['sitecookie'])) )
    {
    header("location: /popular");
    }
    }
    ?>

    <?php
    if(is_front_page())
    {
    setcookie("sitecookie", "CookieValue", time()+10368000);
    }
    ?>

    What's weird is a similar version of this code was previously working even with W3 Total Cache turned on, but I made some changes to the site so I needed to adjust the code and now even if I tried putting the old code back in, the setcookie and redirect no longer work.

  2. savvyharry
    Member
    Posted 2 years ago #

    +1 need to know about this

    @SgrWd, did you find a solution?

  3. adrian7
    Member
    Posted 2 years ago #

    I see a problem with your code:
    <?php
    __the code__
    ?>

    <- blank = output!

    <?php
    __the code__
    setcookie();
    ?>
    There should't be any output before calling setcookie! Have you tried this:

    <?php
    __the code__

    __the code__

    setcookie();
    ?>

  4. Frederick Townes
    Member
    Posted 2 years ago #

    You'll need to make sure the cookie is rejected in the page cache settings.

Topic Closed

This topic has been closed to new replies.

About this Topic