php setcookie not working with ajax call
If you don't add a $path
value to setcookie()
, it defaults to "the current directory". This means that if you set the cookie from /web/DEV/Classes/SetCookie.php
, the cookie gets set to /web/DEV/Classes/
, and anything above that path won't see that cookie.
To fix this, add a specific $path to setcookie. If your app runs on the domain root (example.com), use '/'
. If it's in a subfolder (example.com/myapp/), use '/myapp/'
setcookie("TestCookie", $var, time()+60*60*24*30, '/');
I think you should look into the path parameter of the setcookie. Set it to "/" , so that it is accessible from across all directories/pages of the site.