PHP is a request/response server platform. Users request data from the server maybe in the form of a link on a page, PHP goes out to the server before the new page loads (i.e. server-side) and a response is then sent back to the user. This architecture means that it can be very difficult to automate tasks for your applications as you are relying on user interaction. As we can execute PHP from the command line, I'll show you how to integrate that within a program called Task Scheduler which will allow to execute a PHP file when it matches certain conditions and triggers.
Before we begin, please note that Task Scheduler is a Windows only solution. If your site runs on Linux, you will need to look at daemons and cronjobs which is out of the scope of this guide.
You may have already done this step so skip it if you have. If you haven't, firstly go to Control Panel > System and Security > System > Advanced System Settings
.
Next click on Environment Variables (shown below).
Next click on the Path under System Variables and click Edit. Then you need to add in the path to your PHP folder. In my case that would be C:\\Apache\\php
.
All variables declared in the field are separated by a semi-colon (;) so do not forget this when adding in your path. After you have added your PHP path, click OK, OK then Apply and then restart your computer. Once restarted you have successfully added your environment variable for PHP on your machine.
This is the easy bit. I have already written a simple PHP script that will create a new text file each day, append the date to it's filename and write a small string inside it. Nothing too complicated here. I have saved this in the root of my web projects folder but it could be located anywhere.
$today = date('Y-m-d');
$filename = 'text-file-' . $today . '.txt';
$file = fopen($filename, 'w');
fwrite($file, "Log for $today...");
fclose($file);
Open up Notepad and type the following:
# automatic.bat
php C:\\Apache\\htdocs\\automatic.php
The first part indicates that we want to execute a PHP script and the second part indicates the location of where my PHP file is located. In my case as mentioned earlier, in the root of my web projects folder. Now save the document as automatic.bat
in the same location as your PHP file (doesn't have to be but for simplicity's sake).
To test what we have done so far, double-click the automatic.bat
file you just created and you should see a text file created in the same directory with the date appended to the file name. Now we know that this is working, we can move on to the final step.
automatic.bat
file we created earlier and I've added in the Start In parameter as my web projects folder. This is because when this executes; as I haven't specified a full path in my PHP script as to where my text file gets created; it will create it in the same folder that the script resides in or is executed from.DONE!
You have successfully setup your task.
You are able to test that your script works by right-clicking on the task in Task Scheduler and clicking Run. Similarly, you can edit the rules of the task by clicking Properties.
I am no expert using Task Scheduler or the Command Line but hopefully this will help some users wanting to automate simple PHP tasks for their applications. It must be said that you are also able to write MySQL queries in your script file so you are not limited in terms of the type of automated scripts you want.