I would like to have my session work in my website when using www. AND when not using it. I\'ve read this thread: PHP cookie problem - www or without www
And this would
Using the correct hostname across all requests is important for sessions. However, if you are going to be accessing cookies across multiple subdomains then you can specify the domain parameter with a prepended period. I.e.,
.example.com
Use session_set_cookie_params function before calling session_start, it allows you to set the session domain and other things, set the domain to your domain prefixed with a . to make the session available to subdomains as well.
You can reflect php.ini
for this. Add this in php.ini so that your session cookie will be saved at the place to be accessible with or without www
session.cookie_domain = .example.com
You can also try an alternate to do this
ini_set("session.cookie_domain", ".example.com");
And you can get the host name using $_SERVER['HTTP_HOST']
variable.
PHP uses cookies for the session id, so thats really the same problem (and solution). Have a look at the session configuration.
http://php.net/manual/en/session.configuration.php
I've experienced this problem with my cookies and your link in your post is great solving that.
So far, I'v never experienced difficulties with Sessions. It's independent from domain or sub domains, they are stored on the server-side.
You can Set in a config file a constant parameter DOMAIN_NAME, or in the DB, in prevision for your cookies, and then modify it only once.