• 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 - 1 through 15 (of 34 total)
  • Plugin Support qtwrk

    (@qtwrk)

    did the demo code work for you ?

    and which part it didn’t get executed ? is add_custom_js function being executed besides last line ?

    Thread Starter malswe

    (@malswe)

    Does that code work for you?

    Add the code to your website with your IP address in the IP look-up table.

    Then warm-up cache by visiting, when you press back you should get a “Are you sure do you wanna leave?” pop-up.

    Then visit the site with a non-listed IP address. If the code works, you should not get the “Are you sure do you wanna leave?” pop-up when pressing back.

    Only IP address in the list shall get the javascript. But LiteSpeed caches it on-site if triggered on warm-up.

    _____
    add_custom_js is only being executed there.

    Where can I get the demo code?

    Plugin Support qtwrk

    (@qtwrk)

    Thread Starter malswe

    (@malswe)

    Did my code work for you?

    Thread Starter malswe

    (@malswe)

    The demo works for me

    Plugin Support qtwrk

    (@qtwrk)

    yes, your code works on me , except I just return true; directly on the ip match function…

    Thread Starter malswe

    (@malswe)

    Hmm I don’t know what that means.

    So the pop-up only appears for the listed address for you? And not just whatever was warmed-up with cache?

    Thread Starter malswe

    (@malswe)

    To clarify my issue – the pop-up shall never appear if the visitor’s IP address is not in the list.

    Thread Starter malswe

    (@malswe)

    (Code works great with LiteSpeed Cache Plugin deactivated)

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

    (@qtwrk)

    
    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) {
    	return true;
        $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;
    }
    

    I tested it like this , since I won’t have 192.x IP , so I just make it true

    Thread Starter malswe

    (@malswe)

    But then it will be cached as always true? And the Javascript will always appear?

    Test it like this:
    1. Set your own IP in the look-up table
    2. Warm-up cache, and see the pop-up
    3. Change to a random IP with a VPN
    4. Visit again – If it works as it should, you should not get the pop-up. But right now the cache still delivers the pop-up.

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

    (@qtwrk)

    I see, you need to move your IP match check inside of ESI block , if you put it in add_custom_js it cached by whoever-accessed-first, like so

    
    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;
                setcookie('poc', '1', time() + 31536000, '/');
                break;
            }
        }
    
        if (!$matchFound && (!isset($_COOKIE['poc']) || $_COOKIE['poc'] != '1')) {
            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) {
    	return true;
        $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;
    }

    this way , the IP check part won’t be cached.

    • This reply was modified 1 year ago by qtwrk.
    Thread Starter malswe

    (@malswe)

    Hmm I still get the Javascript injected into page with non-listed IP’s.

    Thanks for helping btw! ??

    Plugin Support qtwrk

    (@qtwrk)

    my previous code comes with

    `

    function ipMatch($userIp, $ipEntity) {
    	return true;

    ——

    update: I don’t think you can set-cookie in ESI block though.

    • This reply was modified 1 year ago by qtwrk.
    Thread Starter malswe

    (@malswe)

    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?';
            '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) {
            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;
    }

    I removed the cookie logic, but still it does not work.

    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)

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

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