user2568495
user2568495

Reputation:

Cookie set in subfolder isn't set in root - PHP

I'm trying to set a cookie in a subfolder /admin/setcookies.php . I'm using this code for doing that:

setcookie(
    "username",
    $myusername,
    time()+60*60*24*365,
    "/",
    $_SERVER['SERVER_NAME'],
    1
);

Now when I test if my cookies are set from the root : /testcookies.php , I can see they aren't actually set. And when I do the same from /admin/testcookies.php , they are in fact set.

What am I doing wrong? The domain name is correct, and the path is set to the root... I don't know what else could be wrong at this point.

Upvotes: 3

Views: 754

Answers (1)

RandomSeed
RandomSeed

Reputation: 29769

The 6th parameter of setcookie() is set to true:

Indicates that the cookie should only be transmitted over a secure HTTPS connection from the client

Your page at /testcookies.php must be accessed through HTTPS for the browser to send such a cookie.

Upvotes: 1

Related Questions