检测用户代理然后将其设置为cookie有时会导致重定向循环

我有以下情况:</ p>


  • 我有两个网站,一个用于移动( m.mydomain.com </ strong>),一个用于桌面( mydomain.com </ strong>)</ p> </ li>
    < li>

    我决定使用用户代理检测来加载哪个站点</ p> </ li>

  • 一旦发现此检测,我将用户的设备信息保存在名为:device的 cookie中。 / strong> </ li>
  • 此Cookie可以包含</ strong>以下值之一:桌面</ strong>或移动。</ strong> </ li >
  • 如果用户</ strong>带有设备cookie ,我会跳过用户代理检测</ strong>,我认为用户确实正在使用设备 写在cookie中。</ li>
    </ ul>
  • </ p>

    (注意 image:它的工作方式相反 当用户想要从桌面打开移动链接时也是如此。 当移动网站设置cookie时,cookie域将为m.mydomain.com)</ p>

    错误:</ strong>
    此时此机制所处的大部分时间都在 同步,但有时我会遇到重定向循环,我真的不明白其原因。 基本上一个域认为它是桌面,另一个域认为它是移动的,并且它们通过重定向发挥无限循环。 在3次重定向后,浏览器崩溃时出现了太多的重定向错误。</ p>

    同样重要的是要提到我从未在真正的移动设备上体验过这种情况,只有在我切换到Chrome检查器之间时才会这样。 我的想法是:</ strong> </ p>


      在重定向之前</ li>
    • 当我想访问该值时,域命名可能会以某种方式引起混淆。</ li>
    • 也许读取cookie值失败</ li>
      </ ul>
      </ div>

展开原文

原文

I have the following scenario:

  • I have two websites, one for mobile (m.mydomain.com) and one for desktop (mydomain.com)

  • I decide which site to load using user agent detection

  • Once this detection happened, I keep the user's device information in a cookie called: device.
  • This cookie can contain one of the following values: desktop or mobile.
  • If the user comes to the site with a device cookie, I skip the user agent detection and I assume that the user is really using the device what is written in the cookie.

enter image description here

(Note to the image: It works the opposite direction as well, when the user wants to open a mobile link from desktop. When the mobile site sets the cookie, the cookie domain will be m.mydomain.com)

The error: So most of the times this mechanism is in sync but still sometimes I experience redirect loops, and I don't really understand the reason of that. Basically one domain thinks that it is desktop, the another thinks it is mobile and they play an infinite loop with the redirection. After 3 redirects the browser crashes with too many redirects error.

It is also important to mention that I never experienced this on a real mobile device, only when I was switching in the chrome inspector between mobile and desktop user agents.

My ideas were that:

  • Perhaps the cookie setting failed before the redirect
  • Perhaps the domain naming causes confusion somehow when I want to access the value.
  • Perhaps reading the cookie value fails

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问