Join 1M+ other developers and:
- Get help and share knowledge in Q&A
- Subscribe to topics of interest
- Get courses & tools that help you grow as a developer or small business owner
Why Are PHP Cookies Not Being Created (but are with Wordpress)?
I have a ‘Ubuntu Wordpress 4.7 on 16.04’ droplet (1 click install). PHP-FPM and Varnish have been added. I’ve created an apache conf for the main domain and another for the blogs subdomain, and deactivated the default conf. I’ve edited my local hosts file to point to this server for testing.
Everything appeared to be working fine, until I hit a strange problem with cookies. I can login and use Wordpress just fine - cookies are created as expected, including the PHPSESSID cookie.
But if I create a simple session_start() test script, the PHPSESSID cookie is not created (and the session doesn’t persist).
<?php session_start(); $_SESSION["favcolor"] = "green"; echo $_SESSION["favcolor"] . "<br>"; ?>
<?php session_start(); print_r($_SESSION); ?>
The same script works when tested locally. I’m trying it on the server in the same directory as Wordpress. And I can see the session is being created on the server in /var/lib/php/sessions. But no browser cookies. Same if I test using setcookie(). Same if I test under the main domain.
At first I suspected a conflict with Varnish, but I’ve temporarily bypassed it while testing (using 'return (pass)’ as the first entry in vcl_recv).
Also intriguing is that if I let Wordpress create the PHPSESSID cookie by logging in. Then my session test script DOES work. Until I delete the PHPSESSID cookie, then it won’t create it.
What am I missing? Why is this working with Wordpress but not with a regular php test script..?
These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.×