• Resolved malswe

    (@malswe)


    Hello! I can’t seem to get this code placed inside functions.php to be excluded from cache with ESI.

    Code works as it should when LiteSpeed Cache is disabled.

    Any help please?

    – LiteSpeed Server at Oderland.
    – ESI is enabled in plugin and server.

    Thanks!

    add_action('litespeed_esi_load-custom_js_block', 'custom_js_block_load');
    
    function custom_js_block_load() {
        do_action('litespeed_control_set_nocache'); // Ensures this block isn't cached
        ?>
        <script type="text/javascript">
            window.addEventListener('beforeunload', function(e) {
                e.preventDefault();
                e.returnValue = '';
                var confirmationMessage = '?r du s?ker p? att du vill l?mna sidan?';
                e.returnValue = confirmationMessage;
                return confirmationMessage;
            });
        </script>
        <?php
    }
    
    function add_custom_js() {
    
        $ipList = [
             '192.168.0.*',
             '192.168.1.*',
            
        ];
    
        $userIp = $_SERVER['REMOTE_ADDR'];
        $matchFound = false;
        foreach ($ipList as $ipEntity) {
            if (ipMatch($userIp, $ipEntity)) {
                $matchFound = true;
                setcookie('poc', '1', time() + 31536000, '/');
                break;
            }
        }
    
        if (!$matchFound && (!isset($_COOKIE['poc']) || $_COOKIE['poc'] != '1')) {
            return;
        }
    
        echo apply_filters('litespeed_esi_url', 'custom_js_block', 'Custom JS Block');
    }
    add_action('wp_footer', 'add_custom_js');
    
    function ipMatch($userIp, $ipEntity) {
        $userSegments = explode('.', $userIp);
        $entitySegments = explode('.', $ipEntity);
    
        $numSegments = count($userSegments);
        for ($i = 0; $i < $numSegments; $i++) {
            if ($entitySegments[$i] === '*') {
                continue;
            }
            if ($userSegments[$i] !== $entitySegments[$i]) {
                return false;
            }
        }
        return true;
    }
    
    
    
Viewing 15 replies - 16 through 30 (of 34 total)
  • Plugin Support qtwrk

    (@qtwrk)

    your latest code doesn’t look right-ish to me , at least add_csutom_js is missing

    but please try keep var_dump() the $_SERVER[‘REMOTE_ADDR’] and $matchFound on different IPs first

    Thread Starter malswe

    (@malswe)

    Oh sorry, here it is.

    Not sure what you mean by this:
    but please try keep var_dump() the $_SERVER[‘REMOTE_ADDR’] and $matchFound on different IPs first.

    Forgive me, I am new to this.

    add_action('litespeed_esi_load-custom_js_block', 'custom_js_block_load');
    
    function custom_js_block_load() {
        
        $ipList = [
             '192.168.0.*',
             '192.168.1.*',
        ];
    
        $userIp = $_SERVER['REMOTE_ADDR'];
        $matchFound = false;
        foreach ($ipList as $ipEntity) {
            if (ipMatch($userIp, $ipEntity)) {
                $matchFound = true;
                break;
            }
        }
    
        if (!$matchFound) {
            return;
        }
    
        do_action('litespeed_control_set_nocache'); // Ensures this block isn't cached
        ?>
        <script type="text/javascript">
            window.addEventListener('beforeunload', function(e) {
                e.preventDefault();
                e.returnValue = '';
                var confirmationMessage = '?r du s?ker p? att du vill l?mna sidan?';
                e.returnValue = confirmationMessage;
                return confirmationMessage;
            });
        </script>
        <?php
    }
    
    function add_custom_js() {
        echo apply_filters('litespeed_esi_url', 'custom_js_block', 'Custom JS Block');
    }
    add_action('wp_footer', 'add_custom_js');
    
    function ipMatch($userIp, $ipEntity) {
        $userSegments = explode('.', $userIp);
        $entitySegments = explode('.', $ipEntity);
    
        $numSegments = count($userSegments);
        for ($i = 0; $i < $numSegments; $i++) {
            if ($entitySegments[$i] === '*') {
                continue;
            }
            if ($userSegments[$i] !== $entitySegments[$i]) {
                return false;
            }
        }
        return true;
    }

    [ Please do not bump. ]

    • This reply was modified 1 year ago by malswe.
    • This reply was modified 1 year ago by malswe.
    Plugin Support qtwrk

    (@qtwrk)

    huh ? what’s the issue now? the last code works on me

    Thread Starter malswe

    (@malswe)

    It works the first two times:

    For example:
    First visit: 192.168.0.1 (IP match – pop-up is shown + cache warmed up)
    Second visit: 292.168.0.1 (IP mismatch- no pop-up is shown)
    Third visit: 192.168.0.1 (IP match – no pop-up is shown) (fail)

    Every visit after this never triggers pop-ups, even if there is an IP match.

    Thanks! ??

    • This reply was modified 1 year ago by malswe.
    Plugin Support qtwrk

    (@qtwrk)

    did you see js ?

    Thread Starter malswe

    (@malswe)

    Sorry, could you clarify and send your code? Thanks

    Plugin Support qtwrk

    (@qtwrk)

    no , i mean , when you do test, do you see the javascript in the source code ?

    Thread Starter malswe

    (@malswe)

    First visit: 192.168.0.1 (IP match – JI is injected)
    Second visit: 292.168.0.1 (IP mismatch- NO JS)
    Third visit: 192.168.0.1 (IP match – NO JS)?(fail)

    Plugin Support qtwrk

    (@qtwrk)

    try add var_dump($userIp); before or after the JS , see how does it show in 3rd

    Thread Starter malswe

    (@malswe)

    Can you enlight me on exactly where to add it? I am not sure. Thanks!

    Plugin Support qtwrk

    (@qtwrk)

    like next line after

    do_action('litespeed_control_set_nocache');
    Thread Starter malswe

    (@malswe)

    Hmm still the same issue

    add_action('litespeed_esi_load-custom_js_block', 'custom_js_block_load');
    
    function custom_js_block_load() {
        
        $ipList = [
             '192.168.0.*',
             '192.168.1.*',
    ];
    
        $userIp = $_SERVER['REMOTE_ADDR'];
        $matchFound = false;
        foreach ($ipList as $ipEntity) {
            if (ipMatch($userIp, $ipEntity)) {
                $matchFound = true;
                break;
            }
        }
    
        if (!$matchFound) {
            return;
        }
    
        do_action('litespeed_control_set_nocache'); // Ensures this block isn't cached
    	var_dump($userIp);
        ?>
        <script type="text/javascript">
            window.addEventListener('beforeunload', function(e) {
                e.preventDefault();
                e.returnValue = '';
                var confirmationMessage = '?r du s?ker p? att du vill l?mna sidan?';
                e.returnValue = confirmationMessage;
                return confirmationMessage;
            });
        </script>
        <?php
    }
    
    function add_custom_js() {
        echo apply_filters('litespeed_esi_url', 'custom_js_block', 'Custom JS Block');
    }
    add_action('wp_footer', 'add_custom_js');
    
    function ipMatch($userIp, $ipEntity) {
        $userSegments = explode('.', $userIp);
        $entitySegments = explode('.', $ipEntity);
    
        $numSegments = count($userSegments);
        for ($i = 0; $i < $numSegments; $i++) {
            if ($entitySegments[$i] === '*') {
                continue;
            }
            if ($userSegments[$i] !== $entitySegments[$i]) {
                return false;
            }
        }
        return true;
    }
    Plugin Support qtwrk

    (@qtwrk)

    yeah , but do you see correct IP in source code when test ?

    Thread Starter malswe

    (@malswe)

    Before I test, I change one of the IP’s in the look-up table to my own IP-Address. That always triggers the pop-up when warming up the cache.
    (Everything works great with LiteSpeed Cache deactivated)

    It almost seems like the “non-match IP” page cache replaces the old cache of a “matched IP” cache. – And then the becomes pop-up untriggerable.

    • This reply was modified 1 year ago by malswe.
    • This reply was modified 1 year ago by malswe.
    • This reply was modified 1 year ago by malswe.
    Plugin Support qtwrk

    (@qtwrk)

    That always triggers the pop-up when warming up the cache.

    so I assume you see your IP correctly in source code , what does the IP shows if you go over VPN ?

Viewing 15 replies - 16 through 30 (of 34 total)
  • The topic ‘ESI not working’ is closed to new replies.