dsy48837 2013-10-27 23:18
浏览 113
已采纳

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

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.

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.

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.

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

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. This is not on a localhost/server, its at a hosting company
  3. This cookie should be available across my entire website.
  4. Cookie expiration date is set to one year

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.

<?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:

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.

  • 写回答

1条回答 默认 最新

  • douna4762 2013-10-28 00:40
    关注

    i read your update

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

    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.

    put the thing outside if( is-admin ) expression

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?