donglu3243 2011-11-01 01:22
浏览 47
已采纳

如何进行自动登录,将会话存储在浏览器中

I have seen some social network sites, if you have a user account, and when you open the browser and type the url, you will be directly loged in without inputting the username and password even if you close the computer and restart the browser your can still be automatically loged in which is different form before which you need to log in with your log in details, I think this is not only include the session cookie like the following

    setcookie(session_name(), '', time()-2592000, '/');

but might be more complicated than it. suppose if I use php, can anybody tell me how to implement this function, I assume it's done on the server side, sorry if I didn't make clear description.

  • 写回答

1条回答 默认 最新

  • douziqian2871 2011-11-01 01:49
    关注

    What I do is, I keep user ID and/or username and login hash, md5(userID + username + password) as cookies.

    Upon another visit, take ID from cookie and check login hash against the same formula it was generated. If the same, login automatically.

    Global

    $randomSeperator = '!~!';
    

    First Run...

    $hash = md5($id . $randomSeperator . $username);
    setCookie('id', $id);
    setCookie('username', $username);
    setCookie('hash', $hash);
    

    Second Run...

    $id/$username/$hash = $_COOKIE[][][] .... // Get all 3 cookies
    
    if($hash == md5($id . $randomSeperator . $username) ){
    // Do Autologin
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 帮我写一个c++工程
  • ¥30 Eclipse官网打不开,官网首页进不去,显示无法访问此页面,求解决方法
  • ¥15 关于smbclient 库的使用
  • ¥15 微信小程序协议怎么写
  • ¥15 c语言怎么用printf(“\b \b”)与getch()实现黑框里写入与删除?
  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教