当用户关闭浏览器时,保持与数据库mysql的通信


Keep communication with database mysql when the browser closed by user

我现在需要同志们的帮助。我正在做基于php的应用程序开发,该应用程序是问卷调查系统,该系统有倒计时计时器60分钟时,用户已经开始(点击开始按钮)。到目前为止,我可以在用户在会话登录时向数据库推送一个计时器,我每秒更新数据库。而目前的系统是当用户注销时倒计时计时器会停止,例如用户注销时倒计时在30分钟内。所以当用户再次登录时,仍然需要30分钟。

但是我的客户不会喜欢的。我的客户端想要如果用户已经点击开始按钮,所以倒计时计时器将持续到60分钟,即使用户没有登出,直到60分钟或用户关闭浏览器,直到60分钟。

所以我的问题是如何实现它?当用户点击按钮开始时,我必须做什么?我想当用户点击按钮开始将推倒计时定时器到数据库mysql,虽然用户关闭浏览器倒计时总是推到数据库。

结构数据库表为:

- id (int) auto_increment;
- user_id (int);
- residual_time (int); <--I push it per second, so per second will update it until 0.

我的服务器使用linux ubuntu服务器。而I是服务器的所有者。

谢谢。

我会完全按照@Joachim的建议去做,并遵循以下流程:

  • residual_time替换为start_time,类型为datetime
  • 添加last_update类型为datetime
  • 当用户点击开始设置start_timelast_updateNOW() MySQL函数
  • 写一个函数,并运行它与AJAX每30?第二,检查last_update是否超过start_time 60分钟,如果是,停止问卷调查并清除start_timelast_update

现在让我们假设一个用户退出并在分配的时间内注销,然后重新登录继续:

//sudo code example
//check if start_time is set
if(isset(start_time)){
    //check if last_update time is set
    if(isset(last_update)){
        //calculate the time difference in minutes from previous start_time and last_update time
        time_diff = last_update - start_time;
        //if time difference is less than alloted time example 60 minutes
        if(time_diff < 60 minutes){
            //update start_time with current time less time difference for already used time
            start_time = NOW() - time_diff minutes
        //if time difference is greater than the allotted time
        } else {
            //kill the page with the time expired message
            die(Your time has expired);
        }
    //if last_update time is not set
    } else {
        //set start_time to NOW()
        start_time = NOW();
    }
//if start_time is not set
} else {
    //set start_time to NOW()
    start_time = NOW();
}