How to Prevent PHP Script Running More Than Once

How to prevent PHP script running more than once?

Now I check whether the process is running by ps and warp the php script by a bash script:

 #!/bin/bash

PIDS=`ps aux | grep onlytask.php | grep -v grep`
if [ -z "$PIDS" ]; then
echo "Starting onlytask.php ..."
php /usr/local/src/onlytask.php >> /var/log/onlytask.log &
else
echo "onlytask.php already running."
fi

and run the bash script by cron every minute.

Prevent php script from running simultaneously and twice

I'm guessing that two tabs in the same browser count as the same client, the browser will use the same session. And the server will answer requests from the same session sequentially. That is why you can be logged into the same service with multiple tabs. (E.g. have several tabs open in stackoverflow)

Requests from a different session (browser) may be processed simultaneously. I guess this depends on your server.

You can't really prevent the script from being executed twice with a simple lock-file. You can only prevent simultaneous execution, as you have demonstrated.

If you wanted to prevent the same client from executing a script too often you'd need to keep track of the last time they executed the script. (possibly in a cookie / database)

Prevent Code or Function from Executing More Than Once

I have an app that does that.

What we did was create a table in the db called version, and stored a version number in there. When the script is ran, it compared the version number in the database with that in the php script. And perform whatever it needs to "upgrade" it to the new version, and then updates the version number in the database.

Of couse, if the version table does not exist, the code will create it and mark it as storing version zero.

How to prevent multiples instances of a script?

I solved this problem using... sockets. It you can enable php_sockets extention then try it. Here is code sample:

$socket = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
if (false === $socket) {
throw new Exception("can't create socket: ".socket_last_error($socket));
}
## set $port to something like 10000
## hide warning, because error will be checked manually
if (false === @socket_bind($socket, '127.0.0.1', $port)) {
## some instanse of the script is running
return false;
} else {
## let's do your job
return $socket;
}

Bind socket on specific $port is safe operation for concurent execution. Operation system will make sure that there is no other process which bound socket to same port. You just need to check return value.

If script crashes then operation system will unbind the port automatically.

This also can be used in any language. I've tested it wide on perl and php based projects. It stoped parallel execution even when we've added script twice in crontab by mistake.

Prevent executing php script twice

Simple solution - use form keys. Each time you render the form, create a random unique string and append it to the form as hidden field. In addition to that, store this key inside a table with an timestamp. You could also associate this entry with the current user if you want.

After the user post the form, you just have to check, if the key exists inside the table and delete it. If the key don't exists - show some kind of error message.

The good thing about this approach is also, that you prevent csrf attacks.

If you don't want to use an table, you could also store the formkey inside the users session, but doing this would result in additional work, if you want support multiple open browser windows with forms at the same time.

There is also csrf build in, using cookies. You just have to enable it ($config['csrf_protection'] = TRUE;) and use form_open to create your form



Related Topics



Leave a reply



Submit