我有一个WordPress网站,其中设置了cookie,以确定在首页上要显示的图像/文本。它总是在Mac上工作,很少在Windows上

发布于 2025-01-29 13:48:35 字数 2722 浏览 0 评论 0原文

在摘要插件中,我添加了以下代码;

    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 } );

这设置了一个cookie并设置在我们网站首页上显示的文件和标题。

当我在Mac(Safari,Firefox,Chrome)上检查同一页面时,我看到cookie都是设置的,将于今年9月到期。

这是cookie的值:

名称:issuecover 值:/wp-content/uploads/2022/03/220.jpg 域:.domain.org 到期:2022年9月5日 尺寸:45 httponly:false 安全:是的 samesite:无; 上次访问:2022年5月17日TUE,

在Firefox,Chrome和Edge的Windows中出现相同的值 - 但是在Windows中,尽管具有相同的Cookie值,但该站点似乎无法访问Cookie。因此,杂志的封面是空白的图像,标题是空白的,但是当我检查时,实际上,cookie的设置就像在我的Mac上一样。

只是在Mac上,每个浏览器都能访问值并显示适当的文本/图像,并且在Windows上,浏览器无法访问相同的cookie。

有人知道Windows是否对Cookie更挑剔?

注意:该网站位于Cloudflare上,奇怪的是,如果我清除窗户中的缓存并强制刷新,则浏览器可以访问cookie中的信息。如果我几个小时后回来了……尽管看到cookie仍然活跃在浏览器中,但cookie中的信息在第一次之后不会在页面上显示。

感谢这里的任何帮助。非常感谢!

编辑:我添加了代码以添加samesite = lax和在Mac上显示,但是在Windows,Samesite =无。

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

更新:

实际上,我注意到另一个插件发生了JavaScript错误,当我将其删除并清除缓存时,Cookie的Samesite Lax状态实际上在Windows中转移到Windows中,并且在页面上可读取/可访问。但是,真正的测试是在1-2个小时内检查一下,看看它是否仍在行事。我会回来的细节。谢谢。

坏消息:

因此,回到网站,尽管在那里,cookies又有samesite = lax,但页面无法访问,其值无法显示。

httponly是错误的...应该出现。为什么这在Windows中不起作用? Mac和Windows都有正确存储的cookie,但仅在Mac上出现在页面上。帮助!

最终更新

我删除了cookie,并使用全局变量选择了不同的问题和相关内容。

由于某种原因,Cloudflare似乎是在缓存饼干。

因此,感谢大家的帮助!

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!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨的味道风的声音 2025-02-05 13:48:35

我删除了所有与Cookie相关的代码,并最终使用了一个更简单,更快的代码,并带有全局变量。

谢谢大家!

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

Thanks everyone!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文