我正在使用会话来维护网站上不同页面之间的一些变量。虽然当我从 www.example.com/a 移动到 www.example.com/b 时它工作正常,但如果我从 example.com/a 移动到 www.example.com/b 会话变量会丢失
知道我如何可以确保无论有 www 或无 www 都保持会话吗?
该网站在导航到不同页面时会附加一个 www,但即使没有 www,也可以访问这些页面。因此,如果有人手动输入地址并省略该部分,然后导航到页面 b(通过单击链接或提交表单),会话变量就会丢失。
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.
发布评论
评论(3)
合理的方法是将
example.com
重定向到www.example.com
;这样您就可以确保所有访问者都在www.example.com
中,并且抓取工具不会为具有相同内容的两个页面建立索引。您可以通过向访问
example.com
的用户发送301: Moved Permanently
来实现此目的(假设您使用 apache,则必须将其添加到您的.htaccess
代码>):第二个版本:抱歉,这是正确的:
取自此处 http://www.webconfs .com/how-to-redirect-a-webpage.php
The reasonable way to do it is by redirecting
example.com
towww.example.com
; by that you make sure that all your visitors are inwww.example.com
, and crawlers won't index two pages with the same content.You do that by sending a
301: Moved Permanently
to users accessingexample.com
(assuming you use apache, you have to add this to your.htaccess
):Second version:Sorry, this one is right:
Taken from here http://www.webconfs.com/how-to-redirect-a-webpage.php
您需要确保使用
session_set_cookie_params
将 cookie 设置为 .example.com 而不是 www.example.com,特别是参数 3 - 文档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会话 ID 存储在 cookie 中。 Cookie 不会从子域转移到子域(例如 www.somesite.com 到 somesite.com)。您可以使用 session_set_cookie_params()< 更改 cookie 参数/a> 或 php.ini
The session ID is stored in a cookie. Cookies don't transfer over from subdomain to subdomain (e.g. www.somesite.com to somesite.com). You can change the cookie params with session_set_cookie_params() or in php.ini