caching - What is an alternative to PHP's session that works with cache?
Get the solution ↓↓↓I am using PHP session to store some variables that have a short lifespan and also get deleted every time the browser is closed. It however does not work if I serve cached version of the page, which is basically a static HTML page.
Is there an alternative that can work the same way AND is compatible with cache in general?
PS: I'm talking about a WordPress site and cache in general (basically applies to every major cache solution out there)
Answer
Solution:
Cookies and caching are the best alternative for session in wordpress. when it comes to cookies basically there are two types of cookies 1.Session Cookies 2.Persistent cookies.
Now the question is How WordPress Core Uses Cookies ? When we refer to WordPress core, we simply mean the files that make up the open source project, before installing any third-party plugins or themes. It’s WordPress in its natural state as we like to call it.
Now that you know the basics of what a cookie is and the different types, let’s take a look at why and how WordPress core uses them to make all that magic happen behind the scenes. Fun fact: Cookie was originally derived from the term “magic cookie.”
WordPress core uses cookies for two different purposes: 1. Login Cookies Login cookies contain authentication details and are used when a user logs into the WordPress admin dashboard. According to the WordPress Codex, a couple of different session cookies are set:
On login, WordPress uses thewordpress_[hash]
cookie to store authentication details (limited to the /wp-admin/ area).
After login, WordPress sets thewordpress_logged_in_[hash]
cookie. This indicates when you’re logged in and who you are.
When you try to access the back-end of your WordPress site, a check is done to see if the two cookies above exist and haven’t expired. This is what allows you to magically bypass thewp-login.php
screen. рџ?‰
WordPress also setswp-settings-{time}-[UID]
cookies. The ID being your user ID from the WordPress users database table. This stores personal dashboard and admin interface settings.
2. Comment Cookies By default, there are cookies set when someone comments on a blog post (with an expiration of 347 days). This is so if they come back later they don’t have to fill out all the information all over again. The following three cookies are stored:
comment_author_[hash]
comment_author_email_[hash]
comment_author_url_[hash]
However, with recent privacy policy changes due to GDPR, new tools have been introduced by WordPress core to make sure you let users opt-in to these cookies being set. This setting, if not already set, can be enabled under “Settings → Discussion” in your WordPress admin dashboard. Select the option to “Show comments cookies opt-in checkbox.” The popular Akismet plugin also allows you to display a privacy notice.
Cookies and WordPress Caching When it comes to WordPress cache, this is where things get tricky. Caching is essentially the process of storing resources from one request and reusing those resources for subsequent requests. Basically, it reduces the amount of work required to generate a page view. While this is great for performance, it causes a problem when it comes to cookies.
Why? Because cookies are there to perform a certain action, such as keeping the shopping cart populated while you browse around a WooCommerce site. However, if a page is served from cache, neither PHP nor the database does anything, the server simply serves up a static copy of the page.
So what can you do?
- Use JavaScript The first option would be to use JavaScript and update content on a page dynamically. Basically, you have HTML placeholders and use JavaScript to pull in info over an API or ajax call.
An example would be loading a list of posts in the WordPress sidebar by using JavaScript to grab a list of posts over the wp-api and then render them in the sidebar. In that scenario you could update the list of posts without clearing the page from cache since the data is generated dynamically.
This isn’t ideal though, it’s always better to cache if possible in terms of performance. But if you must have some bit of content remain dynamic while the page itself can remain static (served from cache), that’s one way to do it – use JavaScript to pull down the content for that part of the page dynamically via an API/ajax call. However, unless you can hire a WordPress developer to build a custom JavaScript solution or extension of a plugin, this option usually isn’t practical.
- Use Admin-Ajax Calls Admin-ajax.php is not able to be cached, therefore you could use admin-ajax calls. A good example of this is the No Cache AJAX Widgets plugin. It makes admin-ajax calls and therefore doesn’t have to worry about conflicting with server-level or third-party caching solutions.
However, just like with JavaScript, going down this route is typically not feasible for the average user. It can also lead to other performance problems such as high admin-ajax usage and lots of uncached requests.
- Exclude Pages From Cache (When the Cookie is Present) TRY A FREE DEMO Unless you can go down the JavaScript or admin-ajax route, excluding pages from caching when a specific cookie is present is the best way to go. This is typically what we recommend, especially those running highly dynamic sites such as WooCommerce and Easy Digital Downloads. At Kinsta, certain WooCommerce and Easy Digital Downloads pages like cart, my-account, and checkout, are automatically excluded from caching. There is a server-level rule in place so that users automatically bypass the cache when the woocommerce_items_in_cart cookie or edd_items_in_cart cookie is detected to ensure a smooth and in-sync checkout process.
We also listen for the associated logged-in cookies and set the cache to bypass when we detect that someone has logged into WordPress. The prevents the back-end dashboard from accidentally being cached.
By default, we don’t exclude the wp_woocommerce_session_ cookie from caching. Most WooCommerce sites in our experience don’t have any issues. This also improves performance by increasing your cache HIT ratio, while utilizing fewer PHP workers.
However, due to there being many different WordPress theme and plugin configurations, we can exclude the wp_woocommerce_session_ cookie from cache if needed. Just reach out to our support team. The result is that once a user adds a product to their shopping cart, all subsequent requests won’t be served from cache, increasing the usage of PHP workers.
If you need a custom page excluded from cache, feel free to open up a ticket with our support team. Again, you have to be careful when it comes to exclusions. Too many uncached pages could really deteriorate performance. Check out our do’s and don’ts for hosting WordPress membership sites.
Share solution ↓
Additional Information:
Link To Answer People are also looking for solutions of the problem: the browser (or proxy) sent a request that this server could not understand.
Didn't find the answer?
Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.
Similar questions
Find the answer in similar questions on our website.
Write quick answer
Do you know the answer to this question? Write a quick response to it. With your help, we will make our community stronger.