Running a php script via ajax, but only if it is not already running

后端 未结 6 1617
轮回少年
轮回少年 2021-02-13 21:28

My intention is this.

My client.html calls a php script check.php via ajax. I want check.php to check if another script task.php is already being run. If it is, I do not

6条回答
  •  广开言路
    2021-02-13 21:42

    I think your are really overdoing it with all the processes and background checks. If you run a PHP script without a session, then you are already essentially running it in the background. Because it will not block any other request from the user. So make sure you don't call session_start();

    Then the next step would be to run it even when the user cancels the request, which is a basic function in PHP. ignore_user_abort

    Last check is to make sure it's only runs once, which can be easily done with creating a file, since PHP doesnt have an easy application scope.

    Combined:

    
    

    In your javascript you can now call the task.php directly and cancel the request when the connection to the server has been established.

    
    

    Bonus points: You can have the actual code for task.php write occasional updates to $checkfile to have a sense of what is going on. Then you can have another ajax file read the content and show the status to the user.

提交回复
热议问题