douqiaolong0528 2016-10-30 06:39
浏览 34

PHP - 自动销毁会话而不调用PHP页面

Is it possible to automatically destroy the session in PHP, without manually check the session in PHP page.

I've searched for this , but the answers lead me to destroy the session manually.

I need this automatic concept, because right now I'm creating a simple bot on telegram to check whether the room is expired or not. Or the player has reached his maximum time limit to answer the question (e.g. 5 seconds).

I also found a concept to use a cron job that set for every second, for checking the session for each user or player. But i think, it will consume so much memory if i set it to every second, and i think the timeout won't be precise too.

I'm so happy if some of you can share any insight for this.

Thanks guya

  • 写回答

2条回答 默认 最新

  • duanlv5084 2016-10-30 10:10
    关注

    Just store a timestamp in the session during the request where you want the clock to start (eg: when the user is presented with the question). Eg:

    $_SESSION['time'] = time();
    

    Anytime a player subsequently makes a request to your server, compare the session's time stamp with the current timestamp. If it's over the time limit, return a message saying the time has expired. Do it like this:

    if(time() - $_SESSION['time'] > 5)
    {
        // Time's up!
    }
    

    No need to run a cron job in the background if you do it this way as if you think about it the only time you need to check if the time has expired is when the user tries to submit a response. You are either going to accept it or not. If the user doesn't submit anything, there's no real point in expiring it via another method.

    You might want to implement a timer in Javascript as well, just so you can show the user that their time has expired. This will be a better user experience. So the timer in Javascript will inform the user, and the $_SESSION method I described above will enforce it on the server side.

    评论

报告相同问题?

悬赏问题

  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图