Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP Session lost when switching from http://domain.com to http://www.domain.com

Tags:

php

session

Hy everyone,

If I visit my website with http://mywebsite.com there is a login page, upon successful login I redirect user to http://www.mywebsite.com instead of http://mywebsite.com.

I noticed that the session has been set but it is lost when switching from http:// to http://www,
though setcookie('name', $data, time()+seconds, '/', ".mywebsite.com") this work on both http:// and http://www Please guide me Thanks

like image 329
geeksalah Avatar asked Feb 18 '26 18:02

geeksalah


1 Answers

You could use session_set_cookie_params to set the cookie domain for the session to .mywebsite.com, or save the session id in your own cookies.

But I would suggest to create an redirect to the www or non-www version of your site in the htaccess

RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
like image 139
Philipp Avatar answered Feb 21 '26 14:02

Philipp



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!