简体   繁体   English

从http://domain.com切换到http://www.domain.com时,PHP会话丢失

[英]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.

相关问题 http to https AND //domain.com to www.domain.com AND subdomain.domain.com to subdomain.domain.com through htaccess - http to https AND //domain.com to www.domain.com AND subdomain.domain.com to subdomain.domain.com through htaccess 继续从www.domain.com重定向到domain.com - keep redirecting from www.domain.com to domain.com 可从domain.com和www.domain.com访问Cookie - Cookies accessible from domain.com and www.domain.com 如何将http://domain.com重定向到https://www.domain.com? - How to redirect http://domain.com to https://www.domain.com? SSL错误:如何通过.htaccess将https://www.domain.com重定向到http://domain.com以避免SSL错误 - SSL Error: how to redirect https://www.domain.com to http://domain.com via .htaccess to avoid SSL error 将 domain.com/shop 重定向到 www.domain.com/shop - Redirect domain.com/shop to www.domain.com/shop 2个不同的php会话:www.domain.com和domain.com:如何整合? - 2 different php sessions: www.domain.com and domain.com: How to consolidate? htaccess将domain.com重定向到www.domain.com,但多个域的staging.domain.com除外 - htaccess redirect domain.com to www.domain.com except of staging.domain.com for multiple domains 如何在wordpress中同时启用domain.com和www.domain.com - how to enable both domain.com and www.domain.com in wordpress Yii ::将www.domain.com/index.php重定向到www.domain.com,而不使用htaccess - Yii:: redirect www.domain.com/index.php to www.domain.com without using htaccess
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM