douxi0098 2016-01-10 01:13
浏览 32
已采纳

PHP - 结束会议

I have read this topic as I was wondering if it's possible to close a session remotely. It is possible I might not be understanding the topic entirely so please let me know.

Ideally if somebody is logged in to my application and then logs again using different PC - I would like to close the other session without using the database.

I've used this:

session_start();
session_id($ns);
session_destroy();
ob_start(); 
session_start();
session_regenerate_id();

but it doesn't actually destroy the session. Am I doing something incorrectly?

-- edit

Forgot to add few details. I only want to contact the database once - when person logs in again - the current session_id is then added to $ns variable.

But I don't want to access the DB on each page. Before I log somebody in I am reading the session from DB and trying to destroy it and start a new one.

Changed order of session_start() and session_id($ns) and still can't do it

  • 写回答

2条回答 默认 最新

  • dqyp50298 2016-01-10 01:22
    关注

    When specifying a session id, session_id() must be called before session_start().

    If you want to log a user out of other logged in sessions when they log in, you're going to have to do further work, most likely involving the database. Otherwise, you have no way of knowing which session to destroy.

    It's generally not necessary to actually destroy the session until it's accessed again, so going through the trouble of getting a session id just so you can destroy it is more work than necessary. But if you really want to do it that way, then you should store the user's session id in the database, and use it to destroy their prior session.

    A better approach would be to store a token of some sort (unique string, login time, or something) in both the session and the database that, on each page access, can then be validated. If the token in the database does not match the token in the session, the session is old/invalid and the user can be logged out at that time.

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

报告相同问题?

悬赏问题

  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测
  • ¥15 ETLCloud 处理json多层级问题
  • ¥15 matlab中使用gurobi时报错
  • ¥15 这个主板怎么能扩出一两个sata口
  • ¥15 不是,这到底错哪儿了😭
  • ¥15 2020长安杯与连接网探
  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么