1

I have a script that sends a mail if a condition is true. In order to run the script I have to visit localhost/index.php. Every time I refresh the webpage the script checks if the condition is true, and if it is, it sends me an email.

How do I make this script run every minute without having to have my browser on refreshing the page every minute?

My current solution is to put this in my PHP file:

<meta http-equiv="refresh" content="60;url=index.php" />
  • 4
    Use a [cron job](http://en.wikipedia.org/wiki/Cron) (or [Windows Task Scheduler](http://windows.microsoft.com/en-in/windows/schedule-task) on Windows). – Amal Murali Jan 20 '14 at 14:18

3 Answers3

4

Use Windows Task Scheduler. It allows you to run scheduled tasks like Cron for *nix.

update

PHP works from the command line like many other server side languages. This means if parameters are required you can pass them as such:

php C:\Path\to\script\script.php param1 param2

param1 and param2 are passed to your script in a similar way that $_POST and $_GET variables are. See PHP command line usage for more details on how this works. But it would mean very little change to your script which is nice.

John Conde
  • 217,595
  • 99
  • 455
  • 496
  • What should the task scheduler do? I don't want it to open Chrome and visit localhost/index.php. –  Jan 20 '14 at 14:23
  • Write your script so it doesn't require a browser to work. PHP can handle command line parameters just fine. – John Conde Jan 20 '14 at 14:24
  • I have no parameters that has to be passed in but I tried this in a .bat file: **C:\wamp\bin\php\php5.4.3\php.exe -f c:\wamp\www\index.php** Which didn't work. The CMD window pops up, runs through all code that exists in index.php, then closes. I am sure the condition is true but no email is being sent. If I visit localhost/index.php, the email gets sent so there is nothing wrong with the script... –  Jan 20 '14 at 14:32
  • What condition needs to be true for the email to be sent? We might need to see some code to figure out why it doesn't work from the command line for you. – John Conde Jan 20 '14 at 14:33
  • Or yes, now something else happens, but CMD complains about undefined variables....................................... –  Jan 20 '14 at 14:37
  • Sounds like your code does depend on things that break if run from the command line. I would post a new question showing the code so we can troubleshoot that for you. – John Conde Jan 20 '14 at 14:42
  • Is it possible to show the part of one file where the error occurs? – John Conde Jan 20 '14 at 15:02
2

You can run a PHP script without the browser by to following line put into

Start -> run

c:\program files\xampp\path\to\php\bin\php.exe -f c:\htdocs\script\script.php

Put this into the Windows Task Scheduler run input.

EDIT: As the OP said they were using WAMP, PHP is actually in this folder structure

\wamp\bin\php\phpx.y.z\php.exe
RiggsFolly
  • 93,638
  • 21
  • 103
  • 149
Daniel W.
  • 31,164
  • 13
  • 93
  • 151
0

In windows you need Task Scheduler for that.

you need to run php.exe file in some regular interval and that file will execute your php script for example,

C:\Path\to\php.exe -f "C:\Path\to\file.php"

thus, with Windows Task Schedular you can run php.exe file in some regular interval with specified condition. Here are the steps,

1. Open Task Scheduler

2. In Task Scheduler, Click on "Action -> Create Task" from menubar.

3. In "General" tab write "Name"- Name of Task and "Description" - Description for Task

4. In "Triggers" tab click on "New" and give triggering time as per requirement.

5. In "Actions" tab click on "New" and select Action - Start Program (which is Default). And from browse button select your php.exe file from your php installed files. And in "argument" give file path with Option ex. -f c:\Path_To_Htdocs\fileName.php (here -f is option).

6. In "Conditions" tab you can set condition when to run task means about the your pc status like idle/on AC power, etc. I suggest you to leave it as default.

7. In "Settings" tab you can specify some extra settings. I suggest you to leave it as default.

8. Finally As you done, Click on "Run" from "Action" to run it manually.

Bhavesh G
  • 3,000
  • 4
  • 39
  • 66