aherlambang
aherlambang

Reputation: 14418

scheduling a one time request using PHP

I have a url in my own website that I want to hit at a scheduled time and once, the url will have different parameters, so for example:

www.mysite.com?user_id=1    ==> execute this once 5 minutes from now
www.mysite.com?user_id=2    ==> execute this once 10 minutes from now
www.mysite.com?user_id=3    ==> execute this once 15 minutes from now
www.mysite.com?user_id=4    ==> execute this once 20 minutes from now

How can I do this in php? I was considering to use cron job, but that seemed to be used for repeated actions, while in this case I wanted it to execute once only. Also I was considering to use sleep, however sleep blocks the current execution. Any other alternative to do this?

Upvotes: 2

Views: 243

Answers (2)

invisal
invisal

Reputation: 11181

First of all, have a text file that contains a link that you want to update with timestamp. For example:

1379041200 www.mysite.com?user_id=1 
1379051200 www.mysite.com?user_id=2 
1379101200 www.mysite.com?user_id=3

Append more link and timestamp that you want to execute into file if you want to add more link. Then have a con job that execute once every 5 minutes. The cron job will check if there is any link with timestamp smaller than current timestamp.

Upvotes: 1

dkkumargoyal
dkkumargoyal

Reputation: 556

you can add a wait time parameter and put sleep at the start of you code

www.mysite.com?user_id=1&wait_for=300

if(isset($_GET['wait_for'])) {
    sleep($_GET['wait_for']);
}

This way your actual file code will be executed after a delay

Upvotes: 0

Related Questions