user1894929
user1894929

Reputation: 199

Executing php function every x amount of time for multiple users

got this code:

<?php
function test ($url){
$starttime = microtime(true);
$valid = @fsockopen($url, 80, $errno, $errstr, 30);
$stoptime = microtime(true);
echo (round(($stoptime-$starttime)*1000)).' ms.';

if (!$valid) {
   echo "Status - Failure";
} else {
   echo "Status - Success";
}
}
    test('google.com');
?>

I want to have an option to execute this function every 5mins / 1hour / 1 day etc.. I was suggested using cron, but i never heard of cron before and after doing some research i understood that its a sepperate file, that would exeute the function every x amout of time. What if i would have multiple users, for example userA would want to run the script every 5mins, and userB would want to run the script every hour. In this sittuation i would need to create multiple cron files for each user?

Edit: I was thinking about doing something like this:

for ($i = 0; $i < 100; $i++) { 
    test('google.com');
    sleep(10 * 60); 
}

Only in the sleep line i would have a custom $n field that each user would define by themselves. My problem with this was - it returns results only when the full cycle has finished, i would want it to return result after every "round?" (idk what its called, but basically would give 1 value, then 10mins later 2nd value and so on) .

Upvotes: 0

Views: 305

Answers (2)

Estefano Salazar
Estefano Salazar

Reputation: 419

May be you need use cron (UNIX) or Windows Tasks (Microsoft) depending the OS on your server.

Cron

Windows Task

Greetings,

Upvotes: 0

JoDev
JoDev

Reputation: 6873

The only way I found is to use an ajax method which will be called from the user page within a setTimeout. See : settimeout function

Upvotes: 2

Related Questions