简体   繁体   English

将用户重定向到www.url-$ _SESSION问题

[英]Redirect users to www.url - $_SESSION issue

The Issue 问题

My domain is www.example.com . 我的域名是www.example.com If a user logs in and then closes the tab, then later opens it and goes to www.example.com , the session continues absolutely fine, with the user logged in. 如果用户登录然后关闭该选项卡,然后稍后将其打开并转到www.example.com ,则该会话将在用户登录后继续正常进行。

However, if they type example.com (without the www. ) then they appear logged out, and if I place print_r($_SESSION) nothing comes up. 但是,如果他们键入example.com (不带www。 ),那么它们将显示为注销,并且如果我放置print_r($_SESSION)什么也不会出现。 However, clicking any link on the page (redirecting the user to www.example.com/link ) restores their session. 但是,单击页面上的任何链接(将用户重定向到www.example.com/link )将还原其会话。

Therefore I've realised that the user appears logged out when they type in example.com without the trailing www - something that a lot of users (like myself) do regularly. 因此,我意识到用户在输入example.com出现登出却没有尾随www的情况 -许多用户(例如我自己)经常这样做。

The Question 问题

The two ways I can see of fixing this are either: 我可以通过两种方式解决此问题:

  • Find a way for the $_SESSION to be applicable to any url subdomain (or lack thereof) 找到一种方法,使$_SESSION适用于任何url子域(或缺少该子域)
  • Redirect users who type in example.com to www.example.com 将输入example.com的用户重定向到www.example.com

Are either of these are feasible and, if so, which is the best practice? 这两种方法是否可行?如果是,哪种方法最好?

When the session cookie is set, it uses the current domain. 设置会话cookie后,它将使用当前域。 But you can configure that this cookie should allow for wildcard domain "*.example.com" instead. 但是,您可以配置此Cookie,而应允许使用通配符域“ * .example.com”。 That way the session would continue on either url. 这样,会话将在任一URL上继续。 Be informed that wildcard domain cookies are a little bit unsafer, because they will be sent to ANY subdomain of "example.com" - even those subdomains that you created on some other server. 通知您,通配符域cookie有点不安全,因为它们将被发送到“ example.com”的任何子域-甚至是您在其他服务器上创建的那些子域。 This might not be an issue, but should be mentioned. 这可能不是问题,但应该提及。

But the real fix should be to only have ONE designated domain to be used. 但是真正的解决方法应该是只有一个指定的域可以使用。 Redirect the user to this one domain with a permanent redirect code. 使用永久重定向代码将用户重定向到该域。 That also circumvents the duplicate content issue of search engines (which is not a penalty, but rather the question of "Which of the two URLs should be shown as a search result?"). 这也避免了搜索引擎出现重复的内容问题(这不是惩罚,而是“两个URL中的哪个应显示为搜索结果?”的问题)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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