In such scenario, we also use PHP to check the cookie value and change the product prices accordingly. If you set a cookie with
Have you heard about raw cookie?
No, I haven’t. …Until a week ago. The project I was working on got into a weird problem with cookies. And that led me to a new concept called “raw cookie”.
In short, I used a cookie to save the email address which users input in the lightbox form. It worked fine with normal email addresses. But with the “labeled” email from Gmail, like in this format
email@example.com, I started to get into trouble.
I used PHP variable
$_COOKIE['email'] to get the email. When it’s labeled, I always got something like
I found a thread from Stack Overflow mentioned “raw cookie” in PHP. Values in raw cookies aren’t “urlencoded“. With
Luckily I found another thread on Stack Overflow, which was a real lifesaver this time. The following piece of code shows you how to get raw cookie in PHP:
With such method, I can now get the right value from the cookie with the plus sign. If you do find this help, please upvoted the thread on SO! (Let’s make it a warm place where we’d like to send our kids to ;))
The PHP cookie unfriendly caching system
Cached pages can be loaded way faster. Why? Because the server-side technique (Nginx or Apache modules) pre-saves PHP pages as static HTML files. When visitors come, the browser just loads the static files physically store on the server and get updated periodically.
Now imagine in such server environment, if we use PHP cookie to display the username on the top right corner of the website, it actually introduces problems. Only because the first person came to the site is called “John”, the top right corner will be showing “Welcome! John!” to every later comer.
Typically there are two approaches to solve this problem. First, we can exclude such cookie from the caching system. So when the cookie gets generated, the page caching stops working right away. The server falls back to generate dynamic page content with PHP.
E-Commerce WordPress websites often adopt the first approach. If you ever had similar problems, this great post from Rahul Bansal would be a start for you to solve yours. Since It requires you edit your server configuration, it’s not ideal if you’re just a general WP theme or plugin developer. (Or you just don’t have the right permission.)
It’s time for AJAX to weight in
I personally recommend the second approach because it obvious outperforms the first one. You can still have your pages cached that provides better user experience. But keep in mind, if more content on the page needs to be dynamic, you won’t be able to take too much advantage from it.
What about page fragment caching?
I’ve written a post on page fragment caching with W3 Total Cache a while back ago. I didn’t try but maybe with the same method, you can use PHP cookie with page caching if you use W3 Total Cache.
But with other server side caching mechanisms, like Varnish or Redis, I have no experience and have no clue how they work. If you have related experience, feel free to share your findings in the comments.