탭을 닫는 것이 세션 쿠키를 삭제하지 않는 이유는 무엇입니까?

StackOverflow https://stackoverflow.com/questions/805895

  •  03-07-2019
  •  | 
  •  

문제

내 웹 사이트와 함께 세션 기반 쿠키를 사용하고 있습니다. 놀랍게도, 나는 세션 쿠키 (지속적인 쿠키가 아님)를 설정하고 탭을 닫은 다음 사이트에 다시 연결하면 세션 쿠키가 여전히 존재합니다. 그것은 실제로 내가 기대했던 것이 아닙니다. 세션 쿠키가 삭제 될 것으로 예상했을 것입니다.

브라우저를 닫으면 세션 쿠키가 삭제되므로 동일한 결과를 얻기 위해 탭을 닫지 않겠습니까?

따라서 PHP5와 JQuery를 사용하고 있습니다. 탭이 닫히면이 세션 문제를 해결할 수 있도록 할 수있는 일이 있습니까? 불행히도 Body Tag의 onbeforeunload 이벤트는 여기에서 유용하지 않습니다. 페이지를 클릭하면 탭을 닫는 것이 아니라 해당 이벤트가 발생하기 때문입니다.

도움이 되었습니까?

해결책

세션 쿠키는 창 당 프로세스 당입니다. 새 창을 선택하더라도 여전히 같은 세션 ID를 얻을 수 있습니다. 이 행동은 의미가 있습니다. 사용자가 사이트를 탐색하는 동안 새 창을 열 때마다 사용자가 다시 서명하는 것을 원하지 않을 것입니다.

나는 이것에 대한 진정한 방법을 잘 모른다.

다른 팁

This is by design and trying to change it is a very bad idea. What if a user opens a link in a new tab and closes that? Should the session in the original tab be destroyed? Of course not! This demonstrates why you should not even think about this.

A session ends when the last browser window closes. If you want something else, you:

  1. do not want sessions;
  2. need to make your own "mini-session" infrastructure;
  3. are probably in for a world of hurt and bugs.

You can also write a javascript that detects when a tab is closed and delete the cookie in the javascript

Session web storage can be used instead of cookies if you need to depend on tab closure.

I found a work around.

I'm working in ASP.NET C#. I have a Master Page for all the pages of the site except for the Login page. In the Master Page Sever Page Load event I get the Url of the referring page and check if it contains the root of the the site, if not I redirect to the Login page and since it doesn't have that Master Page it displays.

This works if I try to get to a page from another site or if I enter the Url to the address box of the browser. So if you close the tab and you try to reenter from another tab or reopen the tab, even tho the cookie hasn't been killed you can't reenter the site without going thru Login. This works also even if you haven't closed the tab and your navigating between different sites in the same tab.

This is the code

   if (Request.UrlReferrer == null || !Request.UrlReferrer.AbsoluteUri.ToString().Contains("root"))
        {
            Response.Redirect("~/Account/Login.aspx");
        }

When navigating from within the site there's no problem even if you open a link to another page in the site to another tab it opens.

If you want to be additionally sure you can kill the session and authentication cookie in that if clause before redircting to the Login page.

This won't work when a user navigated to another site in the same tab and presses the browsers back to button because that works on cache and doesn't automatically send a request to the server.

So this doesn't kill the session or authentication cookie on closing the tab, but it can help prevent reentering the site without logging in after closing the tab.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top