Support » Plugin: W3 Total Cache » IP Check on a cached site

  • Resolved tjldesigns


    Hi there,

    We run a check against the user’s IP address to display some jquery code (or not) within footer.php of our theme.

    The check is done against $_SERVER[‘REMOTE_ADDR’] and what happens is once the site is cached (and a user is not logged in), this check is just not performed for each user because they are seeing a cached/static version of the site.

    Please could you let me know how we overcome situations like this where we need to do a check such as this on a cached site?

    Thanks ever so much for any help/advice you can offer :),

Viewing 5 replies - 1 through 5 (of 5 total)
  • Plugin Support Marko Vasiljevic


    Hello @tjldesigns

    Thank you for your inquiry and I am happy to assist you with this.
    You can use Page Fragment Caching and wrap a PHP code with <!–mfunc –> comment. This way you can serve the dynamic part of the page and keep the rest of the page cached.
    You can check more details about this on our FAQ page and this article.
    The other possibility is to use the Fragment Cache module which was developed in W3 Total Cache for capturing the output of do_action() calls, or when the theme uses our functions to store/get some values. Please keep in mind that Fragment Caching is available after the upgrade.
    Thank you!

    Hi Marko,

    Thanks so much for your quick help, this is so much appreciated and makes complete sense – we will give this a try :).

    Many thanks again,

    Hello again Marko,

    So I followed the example here: with the code below, but sadly that doesn’t seem to execute, it just outputs it exactly as below with the variable names as opposed to the values they should be outputting if dynamically ran.

    <!-- mfunc mysecurestring -->
    echo $_SERVER['REMOTE_ADDR'].' : Date and time: '.date("d/m/Y h:i:sa");
    <!-- /mfunc mysecurestring -->

    Please can you let me know where I’m going wrong? 🙂

    Many thanks again,

    Ah sorry Marko,

    I think I have answered my own question reading further in the article… I need to also complete the actions below, then that should work.

    To be able to use page fragment cache, you define a static variable in your wp-config.php file like define( ‘W3TC_DYNAMIC_SECURITY’, ‘mysecurestring’ );. Also add mfunc and mclude to Ignored comment stems: in the Minify settings if it is enabled.

    So please ignore my last message, I jumped the gun too soon haha!! Many thanks :)!

    Hello again Marko,

    So sorry to bother you again, but sadly made those last changes, but no luck :(.

    This is what I have:

    In wp-config.php:

    define( 'W3TC_DYNAMIC_SECURITY', 'mysecurestring' );

    In the minify settings I have set:

    Ignored comment stems:

    This is then my code:

    <span id="troubleshoot" style="display: none;">IP: 
    <!-- mfunc mysecurestring -->
    echo $_SERVER['REMOTE_ADDR'].' : Date and time: '.date("d/m/Y h:i:sa");
    <!-- /mfunc mysecurestring -->

    But it sadly outputs as below (where the variables are just not firing):

    <span id="troubleshoot" style="display: none;">IP:echo $_SERVER['REMOTE_ADDR'].' : Date and time: '.date("d/m/Y h:i:sa");</span>

    The only other thing I can see at this point is within Fragment Cache it says:

    Fragment caching via disk?> is currently disabled.

    Does the above have anything to do with it not working still? Or is there something else I am still not quite getting right?

    Thanks ever so much again :),

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘IP Check on a cached site’ is closed to new replies.