0

I'm trying to set a cookie which tracks what page the user is on, so that I can forward this in a websocket header, however when I do this:

setcookie("PAGE", $_SERVER['PATH_INFO'], 0, "/");

Which should be setting a cookie to something like %2FCommission%2F1 (/Commission/1), creates the cookie, which shows in firefox developer tools for a split second, then disappears (it doesn't show at all in chrome developer tools).

But if I manually set the cookie value as such:

setcookie("PAGE", "%2FCommission%2F1", 0, "/");

The cookie works perfectly fine.

I have tried trimming $_SERVER['PATH_INFO'], along with replacing potentially problematic parts, but nothing seems to work, if $_SERVER['PATH_INFO'] is used in any capacity in the creation of the string passed into the cookie value, I get this behaviour. Am I missing something?

  • Your not actually setting the cookie by using `0`, your just sending it to be discarded by the browser. If its a websocket, why not get it from `window.location.pathname`? – Lawrence Cherone Jan 07 '18 at 04:16
  • The expiration of 0 actually makes it expire when the user closes their browser. But you make a fair point, I probably could just make the cookie on the client-side. Don't know why I didn't think of that. I still kinda want to get to the bottom of this strange behaviour though. – Josh Cropper Jan 07 '18 at 04:19
  • Oops yeah your right, how is the url structured? as if your using `$_SERVER['PATH_INFO']` it will only contain the value if you have a url like `index.php/Commission/1` (everything after .php) – Lawrence Cherone Jan 07 '18 at 04:22
  • Maybe you're after `$_SERVER['REQUEST_URI']` instead. – Lawrence Cherone Jan 07 '18 at 04:24
  • Are you sure there is a $_SERVER['PATH_INFO']. Because at many servers there is no such thing. Better to use other SERVER values. – halojoy Jan 07 '18 at 04:30
  • There is definitely a $_SERVER['PATH_INFO'], and REQUEST_URI causes the exact same behaviour. Also, the URL is structured domain.tld/OvOcommissions/Commission/1 with PATH_INFO giving /Commission/1 because of my nginx configuration. – Josh Cropper Jan 07 '18 at 04:46

0 Answers0