logged in cookie
logged in cookie

--- a/varnish-conf/v3/default.vcl
+++ b/varnish-conf/v3/default.vcl
@@ -50,8 +50,8 @@
         return(pass);
     }
 
-    # don't cache logged-in users or authors
-    if (req.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
+    # don't cache logged-in users. users logged in cookie you can set in settings
+    if (req.http.Cookie ~ "c005492c65") {
         set req.http.X-VC-GotSession = "true";
         return(pass);
     }
@@ -63,6 +63,7 @@
 
     # don't cache these special pages
     if (req.url ~ "nocache|wp-admin|wp-(comments-post|login|activate|mail)\.php|bb-admin|server-status|control\.php|bb-login\.php|bb-reset-password\.php|register\.php") {
+        set req.http.X-VC-GotUrl = "true";
         return(pass);
     }
 
@@ -111,7 +112,7 @@
     }
 
     # You don't wish to cache content for logged in users
-    if (req.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
+    if (req.http.X-VC-GotSession ~ "true" || beresp.http.X-VC-GotSession ~ "true") {
         set beresp.http.X-VC-Cacheable = "NO:Got Session";
         return(hit_for_pass);
 

--- a/varnish-conf/v3/lib/purge.vcl
+++ b/varnish-conf/v3/lib/purge.vcl
@@ -93,6 +93,7 @@
         unset resp.http.X-VC-Purge-Key-Auth;
         unset resp.http.X-VC-TTL;
         unset resp.http.X-VC-GotSession;
+        unset resp.http.X-VC-GotUrl;
     }
 }
 

--- a/varnish-conf/v4/default.vcl
+++ b/varnish-conf/v4/default.vcl
@@ -53,8 +53,8 @@
         return(pass);
     }
 
-    # don't cache logged-in users or authors
-    if (req.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
+    # don't cache logged-in users. you can set users `logged in cookie` name in settings
+    if (req.http.Cookie ~ "c005492c65") {
         set req.http.X-VC-GotSession = "true";
         return(pass);
     }
@@ -66,6 +66,7 @@
 
     # don't cache these special pages
     if (req.url ~ "nocache|wp-admin|wp-(comments-post|login|activate|mail)\.php|bb-admin|server-status|control\.php|bb-login\.php|bb-reset-password\.php|register\.php") {
+        set req.http.X-VC-GotUrl = "true";
         return(pass);
     }
 
@@ -113,25 +114,22 @@
     }
 
     # You don't wish to cache content for logged in users
-    if (bereq.http.Cookie ~ "wp-postpass_|wordpress_logged_in_|comment_author|PHPSESSID") {
+    if (bereq.http.X-VC-GotSession ~ "true" || beresp.http.X-VC-GotSession ~ "true") {
         set beresp.http.X-VC-Cacheable = "NO:Got Session";
         set beresp.uncacheable = true;
         set beresp.ttl = 120s;
-        return (deliver);
 
     # Varnish determined the object was not cacheable
     } else if (beresp.ttl <= 0s) {
         set beresp.http.X-VC-Cacheable = "NO:Not Cacheable";
         set beresp.uncacheable = true;
         set beresp.ttl = 120s;
-        return (deliver);
 
     # You are respecting the Cache-Control=private header from the backend
     } else if (beresp.http.Cache-Control ~ "private") {
         set beresp.http.X-VC-Cacheable = "NO:Cache-Control=private";
         set beresp.uncacheable = true;
         set beresp.ttl = 120s;
-        return (deliver);
 
     # You are respecting the X-VC-Enabled=true header from the backend
     } else if (beresp.http.X-VC-Enabled ~ "true") {

--- a/varnish-conf/v4/lib/purge.vcl
+++ b/varnish-conf/v4/lib/purge.vcl
@@ -94,6 +94,7 @@
         unset resp.http.X-VC-Purge-Key-Auth;
         unset resp.http.X-VC-TTL;
         unset resp.http.X-VC-GotSession;
+        unset resp.http.X-VC-GotUrl;
     }
 }
 

--- a/vcaching.php
+++ b/vcaching.php
@@ -67,6 +67,10 @@
 
         // send headers to varnish
         add_action('send_headers', array($this, 'send_headers'), 1000000);
+
+        // logged in cookie
+        add_action('wp_login', array($this, 'wp_login'), 1000000);
+        add_action('wp_logout', array($this, 'wp_logout'), 1000000);
 
         // register events to purge post
         foreach ($this->getRegisterEvents() as $event) {
@@ -456,6 +460,18 @@
         }
     }
 
+    public function wp_login()
+    {
+        $cookie = get_option($this->prefix . 'cookie');
+        setcookie($cookie, 1, time()+3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false, true);
+    }
+
+    public function wp_logout()
+    {
+        $cookie = get_option($this->prefix . 'cookie');
+        setcookie($cookie, null, time()-3600*24*100, COOKIEPATH, COOKIE_DOMAIN, false, true);
+    }
+
     public function admin_menu()
     {
         add_action('admin_menu', array($this, 'add_menu_item'));
@@ -492,6 +508,18 @@
                     submit_button();
                 ?>
             </form>
+            <script type="text/javascript">
+                function generateHash(length, bits, id) {
+                    bits = bits || 36;
+                    var outStr = "", newStr;
+                    while (outStr.length < length)
+                    {
+                        newStr = Math.random().toString(bits).slice(2);
+                        outStr += newStr.slice(0, Math.min(newStr.length, (length - outStr.length)));
+                    }
+                    jQuery('#' + id).val(outStr);
+                }
+            </script>
         <?php elseif($_GET['tab'] == 'console'): ?>
             <form method="post" action="index.php?page=<?=$this->plugin?>-plugin&amp;tab=console">
                 <?php
@@ -504,7 +532,7 @@
             <h2><?= __('Statistics', $this->plugin) ?></h2>
 
             <div class="wrap">
-                <?php if ($_GET['showinfo'] == 1 || trim($this->statsJsons) == ""): ?>
+                <?php if ($_GET['info'] == 1 || trim($this->statsJsons) == ""): ?>
                     <div class="fade">
                         <h4><?=__('Setup information', $this->plugin)?></h4>
                         <?= __('<strong>Short story</strong><br />You must generate by cronjob the JSON stats file. The generated files must be copied on the backend servers in the wordpress root folder.', $this->plugin) ?>
@@ -595,6 +623,7 @@
         }
         add_settings_field($this->prefix . "override", __("Override default TTL", $this->plugin), array($this, $this->prefix . "override"), $this->prefix . 'options', $this->prefix . 'options');
         add_settings_field($this->prefix . "purge_key", __("Purge key", $this->plugin), array($this, $this->prefix . "purge_key"), $this->prefix . 'options', $this->prefix . 'options');
+        add_settings_field($this->prefix . "cookie", __("Logged in cookie", $this->plugin), array($this, $this->prefix . "cookie"), $this->prefix . 'options', $this->prefix . 'options');
         add_settings_field($this->prefix . "stats_json_file", __("Statistics JSONs", $this->plugin), array($this, $this->prefix . "stats_json_file"), $this->prefix . 'options', $this->prefix . 'options');
         add_settings_field($this->prefix . "debug", __("Enable debug", $this->plugin), array($this, $this->prefix . "debug"), $this->prefix . 'options', $this->prefix . 'options');
 
@@ -607,6 +636,7 @@
             register_setting($this->prefix . 'options', $this->prefix . "hosts");
             register_setting($this->prefix . 'options', $this->prefix . "override");
             register_setting($this->prefix . 'options', $this->prefix . "purge_key");
+            register_setting($this->prefix . 'options', $this->prefix . "cookie");
             register_setting($this->prefix . 'options', $this->prefix . "stats_json_file");
             register_setting($this->prefix . 'options', $this->prefix . "debug");
         }
@@ -675,9 +705,21 @@
     public function varnish_caching_purge_key()
     {
         ?>
-            <input type="text" name="varnish_caching_purge_key" id="varnish_caching_purge_key" size="100" value="<?php echo get_option($this->prefix . 'purge_key'); ?>" />
+            <input type="text" name="varnish_caching_purge_key" id="varnish_caching_purge_key" size="100" value="<?php echo get_option($this->prefix . 'purge_key', 'ff93c3cb929cee86901c7eefc8088e9511c005492c6502a930360c02221cf8f4'); ?>" />
+            <span onclick="generateHash(64, 0, 'varnish_caching_purge_key'); return false;" class="dashicons dashicons-image-rotate" title="<?=__('Generate')?>"></span>
             <p class="description">
-                <?=__('Key used to purge Varnish cache. It is sent to Varnish as X-VC-Purge-Key header. Use a SHA-256 hash.<br />If you can\'t use ACL\'s, use this option.', $this->plugin)?>
+                <?=__('Key used to purge Varnish cache. It is sent to Varnish as X-VC-Purge-Key header. Use a SHA-256 hash.<br />If you can\'t use ACL\'s, use this option. You can set the `purge key` in lib/purge.vcl.<br />Search the default value ff93c3cb929cee86901c7eefc8088e9511c005492c6502a930360c02221cf8f4 to find where to replace it.', $this->plugin)?>
+            </p>
+        <?php
+    }
+
+    public function varnish_caching_cookie()
+    {
+        ?>
+            <input type="text" name="varnish_caching_cookie" id="varnish_caching_cookie" size="10" maxlength="10" value="<?php echo get_option($this->prefix . 'cookie', 'c005492c65'); ?>" />
+            <span onclick="generateHash(10, 0, 'varnish_caching_cookie'); return false;" class="dashicons dashicons-image-rotate" title="<?=__('Generate')?>"></span>
+            <p class="description">
+                <?=__('This module sets a special cookie to tell Varnish that the user is logged in. This should be a random 10 chars string [0-9a-z]. You can set the `logged in cookie` in default.vcl.<br />Search the default value <i>c005492c65</i> to find where to replace it.', $this->plugin)?>
             </p>
         <?php
     }
@@ -687,7 +729,7 @@
         ?>
             <input type="text" name="varnish_caching_stats_json_file" id="varnish_caching_stats_json_file" size="100" value="<?php echo get_option($this->prefix . 'stats_json_file'); ?>" />
             <p class="description">
-                <?=sprintf(__('Comma separated relative URLs. One for each IP. <a href="%1$s/wp-admin/index.php?page=vcaching-plugin&tab=stats&showinfo=1">Click here</a> for more info on how to set this up.', $this->plugin), home_url())?>
+                <?=sprintf(__('Comma separated relative URLs. One for each IP. <a href="%1$s/wp-admin/index.php?page=vcaching-plugin&tab=stats&info=1">Click here</a> for more info on how to set this up.', $this->plugin), home_url())?>
             </p>
         <?php
     }

comments