通过PHP设置Cookie适用于Chrome,但不适用于Firefox或IE

时间:2021-12-05 17:05:58

I have an issue where setting a cookie works perfectly in chrome but not IE or firefox. All the browsers are the current version and I am on a Windows 8 machine. I am trying to set a cookie which lasts for a year.

我有一个问题,设置一个cookie在chrome中完美运行,但不是IE或Firefox。所有浏览器都是当前版本,我在Windows 8机器上。我正在尝试设置一个持续一年的cookie。

I can verify that IE and firefox are receiving other cookies from my site created by other plugins and 3rd party services but it just does not want to accept the cookie I am trying to set.

我可以验证IE和Firefox正在从我的网站接收其他插件和第三方服务创建的其他cookie,但它只是不想接受我试图设置的cookie。

Via firebug in Firefox, I tried to view rejected cookies but none are listed and it is like my cookie just does not want to work outside of chrome.

通过Firefox中的firebug,我试图查看被拒绝的cookie,但没有列出,这就像我的cookie只是不想在chrome之外工作。

This code is in a functions.php file inside of my wordpress website:

此代码位于我的wordpress网站内的functions.php文件中:

add_action( 'init', 'visitor_cookie');
function visitor_cookie() {
    if (!isset($_COOKIE['returning_visit'])) {
        $traffic_type = 'hd';
        $timestring = microtime();
        $pieces = explode(" ", substr($timestring, 2));
        $pieces[0] = "1".substr($pieces[0],0,3);
        $visitor_id = $traffic_type.dechex($pieces[1]) . dechex($pieces[0]);
        $expire = time()+60*60*24*360;
        $path = '/';
        $domain = parse_url(get_option('siteurl'), PHP_URL_HOST);
        setcookie(
            'returning_visit', 
            $visitor_id, 
            $expire, 
            $path, 
            $domain
      );
    }
}

Notes:

  1. As a note my domain is currently development.mydomain.com which will be moved to mydomain.com
  2. 作为注释,我的域名目前是development.mydomain.com,它将被移动到mydomain.com

  3. This is not on a localhost/server, its at a hosting company
  4. 这不是在本地主机/服务器上,它在托管公司

  5. This cookie should be available across my entire website.
  6. 这个cookie应该可以在我的整个网站上使用。

  7. Cookie expiration date is set to one year
  8. Cookie到期日期设置为一年

In addition to viewing the cookies in my browser, I was also using this code to verify the cookie which again showed output in chrome but no other browser.

除了在我的浏览器中查看cookie之外,我还使用此代码来验证cookie,该cookie再次显示chrome中的输出但没有其他浏览器。

<?php echo "'".$_COOKIE['returning_visit']."'"; ?>

UPDATE

The issue is the cookie is only set when logged in as admin. The cookie is indeed being set. I created a question more specific to wordpress:

问题是只有在以管理员身份登录时才会设置cookie。确实正在设置cookie。我创建了一个更具体的wordpress问题:

Solution

The issue was my host caching the pages. Needed to uncache the pages for the proper php code to run. Caching was disabled for Admins, which is why I got the cookie as admin and not a normal user.

问题是我的主机缓存了这些页面。需要解冻页面才能运行正确的php代码。管理员禁用了缓存,这就是我将cookie作为管理员而不是普通用户的原因。

1 个解决方案

#1


0  

i read your update

我读了你的更新

The issue is the cookie is only set when logged in as admin. The cookie is indeed being set.

问题是只有在以管理员身份登录时才会设置cookie。确实正在设置cookie。

It looks like the event handler

它看起来像事件处理程序

add_action( 'init', 'visitor_cookie');

is fired only when user is an admin..

仅当用户是管理员时才会触发..

I believe on Chrome you work on admin session, on other browser you work as guest.

我相信你在Chrome上使用管理员会话,在其他浏览器上工作。

put the thing outside if( is-admin ) expression

把东西放在外面if(is-admin)表达式

#1


0  

i read your update

我读了你的更新

The issue is the cookie is only set when logged in as admin. The cookie is indeed being set.

问题是只有在以管理员身份登录时才会设置cookie。确实正在设置cookie。

It looks like the event handler

它看起来像事件处理程序

add_action( 'init', 'visitor_cookie');

is fired only when user is an admin..

仅当用户是管理员时才会触发..

I believe on Chrome you work on admin session, on other browser you work as guest.

我相信你在Chrome上使用管理员会话,在其他浏览器上工作。

put the thing outside if( is-admin ) expression

把东西放在外面if(is-admin)表达式