lvyan
V2EX  ›  问与答

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

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

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

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

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

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

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