dongxuan1660 2018-01-10 09:54 采纳率: 100%
浏览 46

在会话被销毁时调用函数/处理程序

i wrote a Website in PHP with user authentification. I used sessions for this. When a user logs in, a flag in a database is set and when he logs out this flag is reseted. This prevents that one user can login multiple times into the Website.

All works fine, but when the user forgets to logout, the session become invalid after a time. When the user now tries to relogin, he will be prevented from login, because the flag is still set.

I need a handler/function that will be invoked, when the session becomes invalid/destroyed. How can i do this?

I know that there is a function called session_set_save_handler but i only need the destroy function. Don't need the other functions and can i access the $_SESSION variables in that function ? Because in this variable the userid is saved, whose flag need to be reseted.

Thanks for help.

  • 写回答

1条回答 默认 最新

  • dongzan7016 2018-01-10 10:10
    关注

    Since PHP has no process that can check your sessions in the background you have to use a cron job to check your stale sessions or else a function that is called any time anyone accesses the site that resets all stale sessions. Neither is ideal.

    Rather than worry about a cron job why not just check the session at login time? Instead of a simple flag use a DATETIME field that you can check for staleness. Call the field something like "last_accessed" and update it to NOW() any time a user does anything on the system. Then when they go to log in from another browser if "last_accessed" < 10 minutes or whatever timeout interval you decide on, don't allow the login, but if it is past that timeout allow the login and refresh the timestamp.

    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)