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.

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top