Page 1 of 1
Getting session variables to work with http:// AND www.
Posted: Wed May 14, 2008 2:33 pm
by Citizen
I remember dealing with this a long time ago, but cant find the solution by searching the forums or google....
How can we get a session variable to work on both:
http://page.com
and
http://www.page.com
Re: Getting session variables to work with http:// AND www.
Posted: Wed May 14, 2008 3:11 pm
by JacobT
I'm sure there's probably a cross-subdomain solution, but in my opinion, it's bad to allow people to switch between www and non-www. I would add a .htaccess mod_rewrite rule to force WWW or remove WWW. You can also set a cookie (which can be cross-subdomain compatible) and use that to check/refresh your session.
This is the mod_rewrite code I use:
Code: Select all
RewriteCond %{HTTP_HOST} !^(www\.|$) [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Good Luck!
Re: Getting session variables to work with http:// AND www.
Posted: Wed May 14, 2008 3:28 pm
by Citizen
Thanks, unfortunately, this method wont work in this case since its for a customer that needs this code of mine to work independent of the rest of the sites

Re: Getting session variables to work with http:// AND www.
Posted: Wed May 14, 2008 4:16 pm
by JacobT
Citizen, can you store the data that's in the session in a DB and then use a cookie to reference it? Then you can set the cookie to be accessible on all subdomains like so:
Code: Select all
setcookie("TestCookie", $value, time()+3600, "/~rasmus/", ".example.com", 1);
would that work?
Re: Getting session variables to work with http:// AND www.
Posted: Wed May 14, 2008 5:23 pm
by Citizen
I think I'll try that. Thanks!
Re: Getting session variables to work with http:// AND www.
Posted: Wed May 14, 2008 5:46 pm
by Zoxive
You can also tell php to set the session cookie the same way.
Code: Select all
<?php
// Very top of file before session_start()
ini_set('session.cookie_domain',".myserver.com");
You can also put this in a .htaccess file, or in the php.ini file directly.