0

How do I fix this problem with $_SESSION variables?

Session variables are present when I am on http://www.example.com, and run this code:

echo 'Printing session variables: <br><br><pre>';
foreach ($_SESSION as $key=>$val)
echo $key. ": ".$val. "<br>";
echo '</pre>';

However, if I am on http://example.com, there are no session variables.

How can I make it so that $_SESSIONs work the same, regardless whether www. is prefixed or not?

I would like the site to default to http://example.com, not to http://www.example.com

crashwap
  • 2,846
  • 3
  • 28
  • 62
  • It may be a duplucate. Refer here: http://stackoverflow.com/questions/6784654/different-session-with-urls-with-www-and-without-www – The Guest Apr 07 '15 at 02:46

2 Answers2

0

Simple answer is add a line to the .htaccess file to force the urls be using either the www. or no www.

MrTechie
  • 1,797
  • 4
  • 20
  • 36
0

From The Guest's comment, I found the correct solution to using an .htaccess file to re-write the URI so that www prefix is re-written to the non-www form, which is a slight variation on the top-rated answer here:

RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example\.com
RewriteRule ^(.*)$ http://example.com/$1 [R=permanent,L]

Original answer defaults to the www.example.com URI form; above variation defaults everything to the non-www URI form.

Community
  • 1
  • 1
crashwap
  • 2,846
  • 3
  • 28
  • 62