0

In snippets plugin, I have added the following code ;

    add_action( 'wp_head', function () { ?>
<script>
    
        function createCookie(cookieName,cookieValue,daysToExpire) {
          var date = new Date();
          date.setTime(date.getTime()+(daysToExpire*24*60*60*1000));
          document.cookie = cookieName + "=" + cookieValue + "; expires=" + date.toGMTString() + "; path=/; domain=humanistperspectives.org; secure";
        }

    function accessCookie(cookieName) {
          var name = cookieName + "=";
          var allCookieArray = document.cookie.split(';');
          for(var i=0; i<allCookieArray.length; i++) {
            var temp = allCookieArray[i].trim();
            if (temp.indexOf(name)==0)
            return temp.substring(name.length,temp.length);
          }
            return "";
        }

function getCookie(cookieName) {
  let cookie = {};
  document.cookie.split(';').forEach(function(el) {
    let [key,value] = el.split('=');
    cookie[key.trim()] = value;
  })
  return cookie[cookieName];
}

    function delCookie(cookieName) {
        cookieValue = "";
          var date = new Date();
          date.setTime(date.getTime()-(3600));
          document.cookie = cookieName + "=" + cookieValue + "; expires=" + date.toGMTString() + "; path=/; domain=humanistperspectives.org; secure";
        }
</script>
<?php } );

This sets a cookie and sets the file and title that displays on the front page of our site.

When I inspect the same page in browsers on my mac (safari, firefox, chrome), I see that the cookies are all set, expiring in september of this year.

Here are the values of the cookie:

Name: issuecover Value: /wp-content/uploads/2022/03/220.jpg Domain: .domain.org Expires: Sept 5, 2022 Size: 45 HttpOnly: false Secure: true Samesite: None; Last accessed: Tue, May 17, 2022

The same values appear in Windows in firefox, chrome and edge — but in Windows, despite having the same cookie values, the site doesn't seem to be able to access the cookies. So the magazine cover is a blank image and the title is blank, etc — but I see when I inspect that yes, in fact, the cookie is set just like on my mac.

It's just that on the mac, every browser is able to access the values and display the appropriate text/image and on windows, the same cookies are not accessible to the browsers.

Does anyone know if Windows is more picky regarding cookies?

NOTE: the site is on Cloudflare and strangely, if I purge the cache and force refresh in windows, the information in the cookie is accessible to the browsers.. but only at that time. If I come back a few hours later... despite seeing the cookies being still active in the browsers, the information in the cookies won't display on the page after the first time.

I appreciate any help here..thanks very much!

EDIT: I added code to add SameSite=Lax and on Mac, that shows up, but on Windows, SameSite=None still.

          document.cookie = cookieName + "=" + cookieValue + "; expires=" + date.toGMTString() + "; path=/; domain=domain.org; secure; SameSite=Lax;";

UPDATE:

ACTUALLY, I noticed there was a javascript error from another plugin and when I removed it, and purged the cache, the cookie's samesite lax status actually transferred over in Windows and the cookies were readable/accessible on the page. However, the true test is to check back in 1-2 hours and see if it's still behaving how I want. I'll come back with the details. THANKS.

BAD NEWS :

So going back to the site, the cookies AGAIN, despite being there, with samesite=lax, are somehow not accessible to the page and their values can't display.

httponly is false... it SHOULD show up. Why is this not working in Windows? Both Mac and Windows have the cookies properly stored but only on Mac do they appear on the page. HELP!

FINAL UPDATE

I've removed the cookies and used global variables to select different issues and relevant content.

For some reason, it seems like Cloudflare is caching cookies.

So thanks for your help everyone!

Aethon
  • 301
  • 1
  • 2
  • 13
  • Which browser you are using in windows, is it cookie enabled, cookie blocker installed? did you try to verify the cookie through `curl -v URL` – abdul rashid May 17 '22 at 11:49
  • In windows the browswers are firefox, chrome and edge. The cookies are enabled. And I don't know what curl -v URL does? How do I do that? – Aethon May 17 '22 at 11:59
  • Try https://stackoverflow.com/questions/9507353/how-do-i-install-and-use-curl-on-windows – abdul rashid May 17 '22 at 12:22
  • What does the console.log(document.cookie) show on windows? – Onki Hara May 17 '22 at 14:25
  • Are you talking about looking in the Storage tab of developer tools? – Aethon May 17 '22 at 17:38
  • ACTUALLY, I noticed there was a javascript error from another plugin and when I removed it, and purged the cache, the cookie's samesite lax status actually transferred over in Windows and the cookies were readable/accessible on the page. However, the true test is to check back in 1-2 hours and see if it's still behaving how I want. I'll come back with the details. THANKS – Aethon May 17 '22 at 17:50
  • NO LUCK! This is so bizarre. Any ideas? Thanks everyone! – Aethon May 17 '22 at 23:07

1 Answers1

0

I removed all cookie-related code and ended up with a much simpler and faster code with global variables.

Thanks everyone!

Aethon
  • 301
  • 1
  • 2
  • 13