Php run script after some time
I have a big problem: Show A user logs in, and the session opens. After that he clicks a button (in a form) which action is "example.php". This example.php should increase one record in MySQL database by 1. How to write this "example.php"? Please help. EDIT: Sorry, I haven't asked what i was supposed to ask. Main problem: After a user clicks a button, the example.php script should execute after a specified time, for example, a 600 sec. The other problem is that the user can click the button and log out, and despite that, the example.php should execute 600 sec later. EDIT (18:48): OK, I've read all your suggestions, but dont't have an idea how to make all of this things work together. I made a form.php, with input type="submit" and action="example.php". I want to: 1. start a javascript timer; 2. increase a value in database after a time, specified earlier in a variable. Please, if it is possible, give me an example, how to do this. void sleep ( int seconds) void usleep ( int micro_seconds) When you want to pause execution in your script, there are two ways you can implement the code. Some people (thankfully very few) choose the first option and write code like this:
While it does work, there are two problems with it. Firstly, time() has a very low precision, only returning the number of whole seconds that have passed, which makes the whole thing quite vague. Secondly, PHP has to sit there looping thousands of times while it waits, essentially doing nothing. A much better solution is to use the one of the two script sleep functions, sleep() and usleep(), which take the amount of time to pause execution as their only parameter. The difference between sleep() and usleep() is that sleep() takes a number of seconds as its parameter, whereas usleep() takes a number of microseconds - millionths of a second - as its parameter. Using either of them is far more accurate than the previous time() loop, and they both have their advantages - sleep() is better if you do not need the accuracy, and usleep() is better if you do. Simple, really! The above script could be rewritten like this:
Or this:
Note that the default maximum script execution time is 30 seconds, but you can use sleep() and usleep() to make your scripts go on for longer than that because technically PHP does not have control during the sleep operation. Want to learn PHP 7? Hacking with PHP has been fully updated for PHP 7, and is now available as a downloadable PDF. Get over 1200 pages of hands-on PHP learning today! If this was helpful, please take a moment to tell others about Hacking with PHP by tweeting about it! Next chapter: Executing external programs >> Previous chapter: Extension functions Jump to: Home: Table of Contents Copyright ©2015 Paul Hudson. Follow me: @twostraws. Executing PHP filesThere are three different ways of supplying the CLI SAPI with PHP code to be executed:
You cannot combine any of the three ways to execute code. As with every shell application, the PHP binary accepts a number of arguments; however, the PHP script can also receive further
arguments. The number of arguments that can be passed to your script is not limited by PHP (and although the shell has a limit to the number of characters which can be passed, this is not in general likely to be hit). The arguments passed to the script are available in the global array $argv. The first index (zero) always contains the name of the script as called from the command line. Note that,
if the code is executed in-line using the command line switch -r, the value of $argv[0] will be A second global variable, $argc, contains the number of elements in the $argv array (not the number of arguments passed to the script). As long as the arguments to be passed to the script do not start with the # This will not execute the given code but will show the PHP usage $ php -r 'var_dump($argv);' -h Usage: php [options] [-f] However, on Unix systems there's another way of using PHP for shell scripting: make the first line of the script start with Example #1 Execute PHP script as shell script
Assuming this file is named test in the current directory, it is now possible to do the following: $ chmod +x test $ ./test -h -- foo array(4) { [0]=> string(6) "./test" [1]=> string(2) "-h" [2]=> string(2) "--" [3]=> string(3) "foo" } As can
be seen, in this case no special care needs to be taken when passing parameters starting with The PHP executable can be used to run PHP scripts absolutely independent of the web server. On Unix systems, the special Example #2 Script intended to be run from command line (script.php)
The script above includes the Unix shebang first line to indicate that this file should be run by PHP. We are working with a CLI version here, so no HTTP headers will be output. The program first checks that there is the required one argument (in addition to the script name, which is also counted). If not, or if the argument was --help, -help, -h or -?, the help message is printed out, using $argv[0] to dynamically print the script name as typed on the command line. Otherwise, the argument is echoed out exactly as received. To run the above script on Unix, it must be made executable, and called simply as script.php echothis or script.php -h. On Windows, a batch file similar to the following can be created for this task: Example #3 Batch file to run a command line PHP script (script.bat) @echo OFF "C:\php\php.exe" script.php %* Assuming the above program is named script.php, and the CLI php.exe is in C:\php\php.exe, this batch file will run it, passing on all appended options: script.bat echothis or script.bat -h. See also the Readline extension documentation for more functions which can be used to enhance command line applications in PHP. On Windows, PHP can be configured to run without the need to supply the C:\php\php.exe or the
php at richardneill dot org ¶ 9 years ago
email at alexander-bombis dot de ¶ 1 year ago
gabriel at figdice dot org ¶ 5 years ago
david at frankieandshadow dot com ¶ 6 years ago
spencer at aninternetpresence dot net ¶ 11 years ago
How to execute code after some time in PHP?php , you add to the database new job with a current timestamp + 600 seconds. Parallel there should be running some job executor that will gather from database all jobs that have timestamp set to timestamp <= NOW() . If it will some records, call specified piece of code and remove/mark as done that jobs.
How to set a delay in PHP?The sleep() function in PHP is an inbuilt function which is used to delay the execution of the current script for a specified number of seconds. The sleep( ) function accepts seconds as a parameter and returns TRUE on success or FALSE on failure.
Is there a wait function in PHP?Description ¶
The wait function suspends execution of the current process until a child has exited, or until a signal is delivered whose action is to terminate the current process or to call a signal handling function.
How do I run a function in PHP every 5 seconds?React PHP is a widely used event loop for PHP. define("INTERVAL", 5 ); // 5 seconds function runIt() { // Your function to run every 5 seconds echo "something\n"; } function checkForStopFlag() { // completely optional // Logic to check for a program-exit flag // Could be via socket or file etc. // Return TRUE to stop.
|