简体   繁体   中英

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

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

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]

I had a similar problem, however, this solution was good for me, perhaps will help others in the future

edit you php.ini

session.cookie_domain = ".exemple.com"

or in you script php

session_set_cookie_params(0, '/', '.example.com');

session_start();

-

-

if you have problems try add this in your php.ini

suhosin.session.cryptdocroot = Off

suhosin.cookie.cryptdocroot = Off

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM