dongweihuai5601 2012-10-28 15:48
浏览 34
已采纳

从第二台计算机删除会话值

I created a login to my site. After an user logged in I store the user ID and an action (=Logged in) in a database and $_SESSION['loggedin']==TRUE. (if log out the action in DB will Logged out and session will destroyed) If somebody wants to log in again with the same username from a second computer I will not allow it because the last row in the database is 'Logged in' and of course the user ID will the same too. So I do not want to allow multiple connection from one username. Now here I want to put a link: "Disconnect the other connection".

For example: I logged in the school but I forget to logout, then I try to log in at home but it will failed because I am logged in (session is alive) another place so if I click "Disconnect the other connection" link I want to delete the session value in the school (change the action value in DB is not enough because if I change it to Logged out the session is still alive and multiple connection will available)

How can I prevent the multiple connection from one username?

  • 写回答

2条回答 默认 最新

  • doujiu8145 2012-10-28 16:23
    关注

    Sounds like what you'd need to do is save something to identify individual connections, like say a number that you make go up by one every time the account logs in.

    For example, say you log in with a user with the ID one, it's the fifth time you log in so you save this to the DB: UserID=1, State="Logged In", ConnectionID=5 To the session you save: $_SESSION['loggedin']=TRUE and $_SESSION['ConnectionID']=5

    When you log in the next time you check if there is a connection for that account that hasn't been logged out if that is the case you either update the existing row to set the state to logged out or if you want to log times create a new entry with the state as logged out (UserID=1, State="Logged Out", ConnectionID=5) and of course you also insert a row for your new connection, connection 6.

    After that all you need is to have code on your site that runs, possibly, every time a page loads where you check if there are any rows in the database that have the same ConnectionID as you have saved in the $_SESSION and if they have an action set as logged out, if that is the case you destroy the session which will log the previous connection out.

    Hopefully this gives you a good idea of how you could possibly do this. I'm sure there are other options as well.

    If you only care about an account only being logged in from one place and you aren't interested about logging when they logged in out out you can just save the UserID and ConnectionID to the database and then just do check if the ConnectionID in the database is the same as the one in the Session, if not, you kill the session.

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

报告相同问题?

悬赏问题

  • ¥15 使用ue5插件narrative时如何切换关卡也保存叙事任务记录
  • ¥20 软件测试决策法疑问求解答
  • ¥15 win11 23H2删除推荐的项目,支持注册表等
  • ¥15 matlab 用yalmip搭建模型,cplex求解,线性化处理的方法
  • ¥15 qt6.6.3 基于百度云的语音识别 不会改
  • ¥15 关于#目标检测#的问题:大概就是类似后台自动检测某下架商品的库存,在他监测到该商品上架并且可以购买的瞬间点击立即购买下单
  • ¥15 神经网络怎么把隐含层变量融合到损失函数中?
  • ¥15 lingo18勾选global solver求解使用的算法
  • ¥15 全部备份安卓app数据包括密码,可以复制到另一手机上运行
  • ¥20 测距传感器数据手册i2c