How to prevent php file from running when it is being processed? [duplicate]

1

How can I prevent a file in php when it is running from being run by another user?

An example:

User X opens the file exe.php its processing time will be in 25 seconds at this time user Y decides to open the exe.php file but it can not be executed because user X is still processing the file.

Is there a solution to this?

    
asked by anonymous 04.04.2018 / 04:00

1 answer

3

You can try to create a temporary file, if it is Linux it could be something like /tmp/exe.php.lock, and delete it when processing ends. But there is a great risk that the file will not be removed if PHP fails half way.

Another way is to create a socket on a well-defined, fixed port. If bind () fails it is because another instance has already created the socket on that port and has not yet left. You can refer to the example link to see how to create the socket; just go to bind (), and do not forget socket_close () at the end.

and close the socket when processing stops. The chance of the socket to stay open if PHP fails halfway is less, or perhaps nonexistent.

    
04.04.2018 / 04:16