PDA

View Full Version : www and no www



timgolding
04-25-2012, 03:37 PM
For some reason my sites session drops when a user moves from www.domain.com to domain.com. Is there a way to ensure that www and no www users use the same session data?

Fou-Lu
04-25-2012, 03:46 PM
Change your session.cookie_domain to .yoursite.com. Easiest to do with session_set_cookie_params. Check the doc and user examples here: http://ca2.php.net/manual/en/function.session-set-cookie-params.php

timgolding
04-25-2012, 04:02 PM
I did



ini_set("session.cookie_domain", ".domain.co.uk");


Seems to work now.... yipee, thanks



I guess this code will stop the sessions working on my development server?

Fou-Lu
04-25-2012, 04:36 PM
Yes it will. You could however use ini_set('session.cookie_domain', '.' . $_SERVER['SERVER_NAME']); and that looks like it should work. Cookies only have a meaning in an application that can use them, so this won't work on the CLI since $_SERVER won't be available, but its irrelevant since CLI cannot use cookie normally with session_start or setcookie; that would require the use of curl or another method to handle anyway.


Perhaps a better approach would be to use a defined constant in a configuration script. This can be changed between the development and production using define('_MY_SITE', '.localhost') and define('_MY_SITE', '.site.com');. Then just use _MY_SITE in the ini_set.

timgolding
04-25-2012, 04:44 PM
Good idea the domain on the development server will be 192.168.1.99. so should i use ini_set('session.cookie_domain', '.192.168.1.99'); ?

Fou-Lu
04-25-2012, 06:39 PM
Good idea the domain on the development server will be 192.168.1.99. so should i use ini_set('session.cookie_domain', '.192.168.1.99'); ?

If you attach to it via http://192.168.1.99 and there is no map between localhost and this address, then use the address. If its mapped to localhost, I'd suggest using .localhost when you can.