lvyan
V2EX  ›  问与答

js 怎么实现关闭网页自动退出登录状态???

  •  
  •   lvyan · Apr 10, 2014 · 8375 views
    This topic created in 4619 days ago, the information mentioned may be changed or developed.
    js怎么实现关闭网页自动退出登录状态???
    21 replies  •  1970-01-01 08:00:00 +08:00
    allce231
    allce231
    Apr 10, 2014
    1
    cookie 有效期为会话 关闭浏览器 cookie就过期了
    loading
    loading
    Apr 10, 2014 via iPhone❤️1
    2
    @allce231 正解
    lz自己想下,用户直接窗口关(一般也这样关吧…)浏览器这个事件不给js的…
    likexian
    likexian
    Apr 10, 2014
    3
    session
    lvyan
    lvyan
    OP
    Apr 10, 2014
    4
    @loading 就是说js是实现不了这个效果吗??
    binux
    binux
    Apr 10, 2014
    5
    1、做一个长链接,链接断开登出
    2、轮训,超时登出
    loading
    loading
    Apr 10, 2014 via iPhone
    6
    如果不采用session的方式,你用ajax方式访问下 /logout 不就好了么!但是你怎么触发这个事件?

    建议阅读:
    http://blog.csdn.net/fangaoxin/article/details/6952954
    lvyan
    lvyan
    OP
    Apr 10, 2014
    7
    @likexian 具体怎么使用那??
    lvyan
    lvyan
    OP
    Apr 10, 2014
    8
    @allce231 我这个页面关闭之后 cookie还在??
    loading
    loading
    Apr 10, 2014 via iPhone
    9
    看完我提供的链接你应该就明白了,cookie和session都有讲,轮询也是session而已,长链接估计你目前还用不来。
    jsonline
    jsonline
    Apr 10, 2014
    10
    在 onbeforeunload 事件里做退出操作。
    lvyan
    lvyan
    OP
    Apr 10, 2014
    11
    @loading 恩 好的。。谢谢,我先看看。。
    lvyan
    lvyan
    OP
    Apr 10, 2014
    12
    @jsonline 给onbeforeunload函数里面写代码它不执行。。
    loading
    loading
    Apr 10, 2014 via iPhone
    13
    @jsonline 哦,忘了这个,这个事件很久前被我抛弃了…

    onbeforeunload浏览器兼容不好,而且如果有开一个页面B,那关掉页面A是登出了,页面B怎么办。

    btw:不知道网上银行这种基本的安全要求是怎么样的,现在用手机看不了
    juicy
    juicy
    Apr 10, 2014
    14
    是不是意味着刷新页面也属于关闭页面的范畴?
    jsonline
    jsonline
    Apr 10, 2014 via Android
    15
    @lvyan 不能写异步的函数,例如ajax
    ETiV
    ETiV
    Apr 10, 2014
    16
    @loading "引用计数"?

    页面打开的时候, cookie设某个值 +1
    页面unload 的时候, 给这个值 -1

    当发现 == 0 的时候, 清掉跟 session 绑定的那个 cookie.
    bigredapple
    bigredapple
    Apr 10, 2014
    17
    cookie 有效期不要设置 退出浏览器不就失效了
    airyland
    airyland
    Apr 10, 2014
    18
    @bigredapple +1
    为什么大家要想得那么复杂。。
    jsonline
    jsonline
    Apr 10, 2014
    19
    @bigredapple 因为楼主说要用 JS…… 这明显是一个 XY 问题 http://coolshell.cn/articles/10804.html
    ensonmj
    ensonmj
    Apr 10, 2014
    20
    websocket
    bakac
    bakac
    Apr 11, 2014
    21
    不设定cookies的过期时间,关闭浏览器就会自动丢失
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Privacy   ·   Solana   ·   2355 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 43ms · UTC 12:54 · PVG 20:54 · LAX 04:54 · JFK 07:54
    ♥ Do have faith in what you're doing.