Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Session not maintained between mysite.com and www.example.com

I am using a session to maintain some variables between different pages on my site. While it works fine when I move from www.example.com/a to www.example.com/b, the session variables are lost if I move from example.com/a to www.example.com/b

Any idea how I can make sure the session is maintained irrespective of www or no www?

The website appends a www as it navigates to different pages but the pages are accessible without that as well. So if someone is manually typing in an address and omit that part, and then navigate to page b (by clicking on a link or submitting a form), the session variables are lost.

like image 773
lostInTransit Avatar asked Mar 23 '26 07:03

lostInTransit


2 Answers

The reasonable way to do it is by redirecting example.com to www.example.com; by that you make sure that all your visitors are in www.example.com, and crawlers won't index two pages with the same content.

You do that by sending a 301: Moved Permanently to users accessing example.com (assuming you use apache, you have to add this to your .htaccess):

Redirect 301 http://example.com/ http://www.example.com/

Second version:

Redirect 301 http://example.com/(.*) http://www.example.com/$1

Sorry, this one is right:

Options +FollowSymlinks
RewriteEngine on

rewritecond %{http_host} ^example.com [nc]
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

Taken from here http://www.webconfs.com/how-to-redirect-a-webpage.php

like image 64
Gabi Purcaru Avatar answered Mar 25 '26 21:03

Gabi Purcaru


you need to make sure the cookie is being set to .example.com not www.example.com using session_set_cookie_params, specifically parameter 3 - the domain in the documentation

like image 20
tobyodavies Avatar answered Mar 25 '26 20:03

tobyodavies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!