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

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

报告相同问题?

悬赏问题

  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料