Set cookie parameters on wp site – PHP not working?

Question

I’ve developed quite some wordpress plugins which use a user system incl. login, logout etc. features using PHP’s $_SESSION superglobal.

Now I’m stuck in a project where the client does some sensitive request authentications using $_SESSION data, hence the need of some custom cookie parameters, like HTTPOnly, SameSite, etc.

I’m just confused now that when I call session_start() on the top of a page, and then, immediately afterwards, do sth like:

setcookie(
  session_name(),
  session_id(),
  [
    'expires' => time() + 600,
    'path' => "https://wordpress.stackexchange.com/",
    'domain' => 'localhost',
    'secure' => true,
    'httponly' => true,
    'samesite' => 'Strict'
  ]
);

The according cookie parameters are NOT set (I can see in the dev tool, under (Chrome) Application -> Cookies -> go to the session cookie of the page), that the according httponly and secure flags are NOT being applied to the session cookie. Why is that so?

Note: The page is in newbie development, there’s no plugin installed, so anything hampering this may come from the wp core..?

No matter where I call this; page templates (first line of code), functions.php, it never works.

UPDATE

I forgot that http headers are obviously sent before any output and placed the cookie callbacks in the middle of functions sending output, so will try to call the function earlier now.. Although I actually am calling the cb at the very top, so npt sure..

0
DevelJoe 8 months 2021-12-31T13:01:37-05:00 0 Answers 0 views 0

Leave an answer

Browse
Browse