blex
blex

Reputation: 25634

Cookie is present in browser, but php $_COOKIE is empty

I have a project that works on a local server but not on my production server, due to cookies not being seen by the server. I've made a minimal version of the code that reproduces the issue on that server:

<?php

if(!isset($_COOKIE['foo'])){
    setcookie('foo', 'bar', time() + 7*24*60*60, '/');
    echo "Cookie was not found, so we just created it.";
} else {
    echo "Cookie was found!";
}

?>

No matter how many times I refresh this page, I always get the "not found" message. Whenever I try to log the $_COOKIE variable, I get an empty Array. However:

I think this is a server configuration issue, since the code works locally, but I have no idea where to look. Has anyone seen this before, do you know what could cause this?

If you need more info, just tell me and I'll add it to my question. Thank you!

Upvotes: 10

Views: 9799

Answers (4)

jberg7777
jberg7777

Reputation: 391

I just dealt with the same issue, my production server was allowing me to create a browser cookie using setcookie('cookieNameHere', $cookieValueHere, time() + (86400 * 365), "/"), but the $_COOKIE variable was always an empty array.

The issue was that my production server blocked direct access to the$_COOKIE variable contents via PHP for security reasons.

My solution was to access the cookie value via JavaScript using the following function:

    function getCookie(cname) {
      var name = cname + "=";
      var decodedCookie = decodeURIComponent(document.cookie);
      var ca = decodedCookie.split(';');
      for(var i = 0; i <ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) == ' ') {
          c = c.substring(1);
        }
        if (c.indexOf(name) == 0) {
          return c.substring(name.length, c.length);
        }
      }
      return "";
    }

I continued to create/update the cookie via PHP.

FYI, I was working on a WordPress site hosted on WP-Engine. See this page for an in depth explanation, and for other options in the event you absolutely need to access a cookie value via PHP (ADMIN-AJAX calls, etc).

Upvotes: 6

deceze
deceze

Reputation: 522145

If there's a cache server or CDN involved, it may be filtering cookies based on a whitelist. This is to improve caching, since each request with a unique set of cookies would need to be regarded as different from other requests and could not be cached (you may receive a different reply from the server based on your cookies, so the cache server cannot serve you the cached response of a previous client). Since lots of services are setting cookies which may be sent to the server (e.g. analytics packages) but have absolutely no influence on the contents of the response, heeding all cookies by default would often completely subvert caching.

Configure the caching server in charge to specifically pay attention to your cookie and to forward it to the origin server. If you have a lot of different cookies, consider giving them a common prefix and whitelist that (e.g. foo-*).

Upvotes: 7

Nenad Stojanovikj
Nenad Stojanovikj

Reputation: 23

I've had similar problem, and it turned out to be HAProxy configuration issue. Do you have any load balancer between the server and the user?

Upvotes: 1

Yavuz YILDIZ
Yavuz YILDIZ

Reputation: 36

I think your problem is the server time. Check your time with time() function and compare with local time. Maybe one of them is wrong.

Upvotes: -1

Related Questions