dongpi9480 2013-06-23 03:03
浏览 19
已采纳

php:虽然浏览器关闭但保存登录

how to create a login system that is still logged in (as long as user wouldn't click log out) although browser is closed?

when using $_SESSION , it was gone when browser is closed. So, we can use $_COOKIE, but it looks like less secure then session.

How yahoo, facebook, twitter or any site create their login system, so when user close the browser, it will stay logged in? Using IP? $_SESSION? $_COOKIE? or what?

Thank you

  • 写回答

5条回答 默认 最新

  • douh9817 2013-06-23 03:11
    关注

    As stated by Oswald, do not use $_SESSION, as this ends when the browser closes.

    Session variables are stored as cookies, so creating your own is not less secure.

    When creating your own COOKIES, you should set the expiration to be a long time, like a year or so, to ensure that the cookie does not expire, and the user will stayed logged in.

    A vast majority of production sites use cookies for session keeping, just make sure that your session keys are random enough that can not be guessed by another client.

    EDIT

    See this link on how to use setcookie.

    setcookie("session_key", "somerandomstringrepresentingasessionkey", time() + 60*60*24*120);
    

    The following will set a session key for your website with the name session_key, this is how you will fetch the data at runtime:

    $session = $_COOKIE['session_key'];
    

    The next part is where your value will be stored, this will be the session key that you will store in the database to be fetched and matched with the user, what the value of $session will now be.

    The next part is the time until it expires, here i have put 60 * 60 * 24 * 120, meaning that the current time, plus 60 seconds, times 60 minutes, times 24 hours, time 120 days. Meaning that in 120 days from that exact moment in time, that specific cookie will expire, even if the browser is closed before that.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(4条)

报告相同问题?

悬赏问题

  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题