[英]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 . 如果我使用http://mywebsite.com访问我的网站有一个登录页面,登录成功后我将用户重定向到http://www.mywebsite.com而不是http://mywebsite.com 。
I noticed that the session has been set but it is lost when switching from http:// to http://www , 我注意到会话已经设置但是从http://切换到http:// www时会丢失
though setcookie('name', $data, time()+seconds, '/', ".mywebsite.com")
this work on both http:// and http://www Please guide me Thanks 虽然setcookie('name', $data, time()+seconds, '/', ".mywebsite.com")
这个工作在http://和http:// www请指导我谢谢
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. 您可以使用session_set_cookie_params将会话的cookie域设置为.mywebsite.com,或将会话ID保存在您自己的cookie中。
But I would suggest to create an redirect to the www or non-www version of your site in the htaccess 但我建议在htaccess中创建一个重定向到您网站的www或非www版本
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 编辑你的php.ini
session.cookie_domain = ".exemple.com" session.cookie_domain =“。exemple.com”
or in you script php 或者你的脚本php
session_set_cookie_params(0, '/', '.example.com'); session_set_cookie_params(0,'/','。example.com');
session_start(); 在session_start();
- -
- -
if you have problems try add this in your php.ini 如果你有问题,请尝试在php.ini中添加
suhosin.session.cryptdocroot = Off suhosin.session.cryptdocroot =关闭
suhosin.cookie.cryptdocroot = Off suhosin.cookie.cryptdocroot =关闭
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.