Automatic time triggered events - PHP

Lucipher6

New Member
Messages
6
Reaction score
0
Points
0
The website I have is used to read from hiscores for a game, and insert to an SQL database, displaying a table of the values in the table for the experience each person has gained. The main purpose is a competition, so it requires updates often.

Currently, it has buttons for manual updates, which are triggered by an IRC script hourly.

I thought of using the date("i") function as that would return 00-59 depending on the minute. As it's only hitting a minute once per hour, it'd only update once per hour. Yet as far as I can see, this requires a page to be manually opened.

So, the question: Is there a way to trigger functions without any user input whatsoever?
 

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
Cron.

Create a cron job in cPanel that runs the script you want at the time you want it to.

Sample cronjob to run a PHP script:
php -q /home/username/path/to/script.php
 

Lucipher6

New Member
Messages
6
Reaction score
0
Points
0
Thanks TechAsh. A few further questions regarding this however.

Would this be able to run 1 specific function rather than an entire page? The update function itself uses a get form, so rather than script.php, could it run script.php?update=group1 for example.

The interface itself confuses me, though. Leaves me unsure what to select.
In the standard options, would choosing: "Minutes: 50, Hour/month/day/weekday: Every hour/month/day/weekday" update every hour at ##:50?

Finally where would the "php -q /home/username/path/to/script.php" go, generated by the "Standard Cron Manager", placed within the script or saved as a seperate file in a directory?

Couldn't see a forum post which answered these so it's probably something simple, thanks for any further help in advance.
 

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
Would this be able to run 1 specific function rather than an entire page? The update function itself uses a get form, so rather than script.php, could it run script.php?update=group1 for example.
That wouldn't work, but with a line or two added to your script "script.php group1" would work (Note the difference). You will need to add something like this to your script to allow this to work.
PHP:
$update = $agrv['1'];

The interface itself confuses me, though. Leaves me unsure what to select.
In the standard options, would choosing: "Minutes: 50, Hour/month/day/weekday: Every hour/month/day/weekday" update every hour at ##:50?
I agree the interface is quite confusing at first. To get the job to run at 50 minutes past every hour you will need to select: '50' in the 'Minute(s)' box, then 'Every Hour' in the 'Hour(s)' box, you should be able to leav the othere as they are.

Finally where would the "php -q /home/username/path/to/script.php" go, generated by the "Standard Cron Manager", placed within the script or saved as a seperate file in a directory?
The "php -q /home/username/path/to/script.php" will go in the box marked 'Command to run"

I hope this helps, and answers all your questions.
 

souradipm

New Member
Messages
45
Reaction score
0
Points
0
to get the specific function thing to work, you would need something like:
PHP:
<?php 
//funtions defined here
$f=$_GET['func'];
switch($f){
case 0:
function_one();
break;
case 1:
function_two();
break;
//etc
}
 ?>

where you would call update.php?func=0 to call just function_one()

PS - that code is just pseudocode so I apologise if I made any errors :p
 
Last edited:

TechAsh

Retired
Messages
5,853
Reaction score
7
Points
38
to get the specific function thing to work, you would need something like:
PHP Code:
<?php
//funtions defined here
$f=$_GET['func'];
switch($f){
case 0:
function_one();
break;
case 1:
function_two();
break;
//etc
}
?>
where you would call update.php?func=0 to call just function_one()

PS - that code is just pseudocode so I apologise if I made any errors :p

That will not work because the $_GET thing is only for browser requests, for the command line (Which is what Cron uses) you need to do as I said in my above post (Use $argv['1'] instead of $_GET, and "script.php argument" instead of "script.php?func=argument".
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
In a cron, you can still use the time and date functions, so you can always determine what function to run in the php file.
Looking at your first post I'ld say you want a cron every minute, but the minimum time is 5 minutes.
 

jgrey1

New Member
Messages
13
Reaction score
0
Points
0
websitepulse is a service that will request one of your pages at regular intervals, thereby triggering the required event
 

marshian

New Member
Messages
526
Reaction score
9
Points
0
websitepulse is a service that will request one of your pages at regular intervals, thereby triggering the required event

In that case, you have to
1) Thrust them
2) Put your file in a public directory, which is not a good idea.
 
Top