Today, I will share a simple and direct small PHP code that is executed at regular intervals. I will close the browser and continue executing the PHP script!
We use a web browser to regularly refresh and use the sleep function to automatically and periodically execute php. As long as php can run.
Because there are few codes, here is an example to explain:
| The code is as follows: |
Copy code |
|
<? Php
Ignore_user_abort (); // close the browser and run the PHP script.
Set_time_limit (20); // time-out period of the program, in seconds; use set_time_limit (0) to enable unlimited execution of the program; when the set_time_limit () function is used to set the running time, the duration of the sleep () function during program execution will be ignored.
$ Interval = 5; // The number of seconds to run. Unit: seconds.
Do {
// Here is the code you want to execute
$ I = 1;
$ Num_file = "number.txt ";
$ Fp = fopen ($ num_file, "r ");
$ Buf = fread ($ fp, filesize ($ num_file ));
Fclose ($ fp );
$ Number = file_get_contents ($ num_file );
$ Number = $ number + $ I;
$ Fp = fopen ($ num_file, "w ");
Fwrite ($ fp, $ number );
Fclose ($ fp );
Echo $ number;
// Waiting time
Sleep ($ interval );
}
While (true );
?> |
The above method does not work if the browser is closed. We can try to use the platform's scheduled task to handle it.
1. windows scheduled tasks
2. linux script program
The code is as follows:
PHP
| The code is as follows: |
Copy code |
|
<? Php
If ($ _ GET ['AC'] = 'stop ')
Exit ();
Sleep (5 );
$ Name = time ();
$ Fp = fopen ("empty", 'w ');
Fwrite ($ fp, time ());
Fclose ($ fp );
$ Fp = fsockopen ('localhost', 80, $ errno, $ errmsg );
Fputs ($ fp, "GET/test/time. php \ r \ n ");
Fclose ($ fp );
?> |
Run http: // localhost/test/time. php in the browser.
In the test folder, a txt file named after the current timestamp is automatically created every five seconds, and the current timestamp is written.
We can find that even if the browser is closed. This script will be executed every five seconds.
In this way, scheduled execution is achieved.
If you want to execute tasks by time point, for example, at one o'clock every day.
You only need to make a small modification.
Of course, this is just a solution. As for actual application, we need to explore it again.
There is also a way to achieve this: using an endless loop, the principle is similar
PHP
| The code is as follows: |
Copy code |
|
<? Php
Ignore_user_abort (true );
Set_time_limit (0); while (1 ){
$ Fp = fopen('time_task.txt ', "a + ");
$ Str = date ("Y-m-d h: I: s"). "n ";
Fwrite ($ fp, $ str );
Fclose ($ fp );
Sleep (1800); // executed once every 30 minutes
If (! File_exists('1.txt '))
Exit (); // create a file 1.txt in the directory. If the file exists, run it all the time. If the file does not exist, exit.
}
?>
This can also be supplemented.
<? Php
Ignore_user_abort (); // close the browser and continue executing the php code
Set_time_limit (0); // Unlimited program execution time
$ Sleep_time = 5; // How long does the execution take?
$ Switch = include 'switch. Php ';
While ($ switch ){
$ Switch = include 'switch. Php ';
$ Fp = fopen('test.txt ', 'A + ');
Fwrite ($ fp, "This is a php blog: phpddt.com $ switch \ n ");
Fclose ($ fp );
Sleep ($ sleep_time );
}
Exit ();
?> |