6

If a logged-out user visits a page, and then revisit it when logged-in, it will show the cached version. There's a closed question similar to this with an answer that suggests to set Cache Control to no-cache. This will affect logged-out users, which defeats the purpose of caching in the first place.

The problem

When using page/browser caching with T3 Total Cache or likely other caching plugins, if you have already visited a page when logged out, you will be presented with the same logged-out version of the page, unless if you refresh the page.

You can reproduce this issue by:

  1. Have caching enabled. Then, in incognito mode, visit a post.

  2. Login to the site and then manually type the URL of that post into the address bar. The logged-out version of the page should appear.

This is obviously an issue you have any features available for logged-in users only (like comments, voting etc)...

Potential solution

The only potential solution I have is to add a query variable to the URL on every request. For example by adding a random value:

$key = rand();
$new_url = esc_url( add_query_arg( 'foo', $key ) );

I know how to manually add this variable to single queries but instead of altering every single instant of links, is there a way how to force this to every URL for logged-in users?

Note: I'm not sure if this is the ideal approach to solve this problem (it feels quite desperate) so feel free to post an answer with a better solution.

8
  • IIRC W3TC has a option for not caching and not showing cached content for logged-in users - I understood your issue correctly, right? Commented Jun 24, 2015 at 17:52
  • 2
    @ialocin Yes, I have caching turned off for logged-in users. The problem is with browser caching. When visitor loads up a page, the page is cached in the browser even when user is logged-in later. My solution is to add a random variable to the browser but that's a pretty wild solution, and I wouldn't know how to force this on all pages for logged-in users.
    – Christine Cooper
    Commented Jun 24, 2015 at 18:03
  • 2
    @ChristineCooper Cache Control header to no-cache is still the solution, just set it only for logged users. if (is_user_logged_in()) { header("Cache-Control: no-cache, must-revalidate"); header("Pragma: no-cache"); header("Expires: Sat, 26 Jul 1997 05:00:00 GMT"); }
    – gmazzap
    Commented Jun 25, 2015 at 16:05
  • 1
    @gmazzap But isn't it so that if a logged out user has already the content cached (via the browser), it will still display the cached version (because it has not expired yet)? Help me understand the technology.
    – Christine Cooper
    Commented Jun 25, 2015 at 18:49
  • mobify.com/blog/beginners-guide-to-http-cache-headers
    – gmazzap
    Commented Jun 25, 2015 at 18:55

2 Answers 2

4
+50

Since you already have the "Don't cache pages for logged in users" option activated, what about selecting cache with validation under Cache Control Policy for "HTML" content?

Explanation: Essentially, the "don't cache pages for logged in users" option should already be sending a no-cache header for your logged users only.

If a user has a cached page in its browser (as a non-logged user) and then tries to access the same page when logged in, the cache with validation option inside your "Cache Control Policy" setting for HTML contents should be sending the must-revalidate tag.

Hence, a logged in user would send no-cache,must-revalidate, always requesting a fresh page.

This is pretty much what @gmazzap explained with headers - but from a W3 Total Cache configuration perspective.

3
  • 1
    You are referring to the third option, right? See: i.imgur.com/wicdFFi.png -- So if I understand this correctly, if a logged-out user revisit the same page, it will validate again and keep cached files. If a logged-in user visits a page they already have cached in their browser, it will revalidate and send fresh content?
    – Christine Cooper
    Commented Jun 27, 2015 at 16:04
  • Exactly, that's it, inside the HTML & XML section. And make sure the Don't cache pages for logged in users is also checked in the General section.
    – E. Serrano
    Commented Jun 27, 2015 at 22:14
  • Changes have been applied, hopefully it should work now. Thank you for clearing this up. I don't know why I find this complex but it's as clear as it could me for me a suppose. ;)
    – Christine Cooper
    Commented Jun 28, 2015 at 15:42
0

From my tests, "Don't cache pages for logged in users" only works with the "basic" disk caching type in W3 Total Cache. Assuming that's true, the only approaches I can think of are to switch the page cache type to Disk: Basic and either enable the "Don't cache for logged in users" setting or use Page Fragments:

Because I'm using a shared host, I can't change the disk caching method. I've elected to redo many of my pages to ajax to pull in data that needs to be dynamically loaded. This approach has the benefit of quick page loads but will obviously involve more development. Hope that helps.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.