Script that auto executes.

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Script that auto executes.

Post by lovelf »

Is there a way to set a PHP timeout for a function server side, like as soon as the script gets a visit, x time from the visit it will execute a function?

Code: Select all

setTimeout(600000,executescript());
Ten minutes after the visit to that page the script would auto execute at the server, is this possible?
crazycoders
Forum Contributor
Posts: 260
Joined: Tue Oct 28, 2008 7:48 am
Location: Montreal, Qc, Canada

Re: Script that auto executes.

Post by crazycoders »

It requires a special way of structuring your code but it can be done, but not the way you think it can be done like in javascript.

1. Create the script that triggers the timeout activity, for example: index.php

2. In that script, establish a connection to the database server of your choice and in a table, insert an entry stating that a script has to run at a specific point in time. For example: INSERT INTO myscheduledtasks ('<script>', time, variable1, variable2)

3. Use a linux daemon (VCRON, CRON, etc (CRONTAB is usually the default on many many web servers and is available in your control panel)) to schedule a task runner that runs every lets say 10 minutes. When the task manager runs, it loads up the tasks in the database and runs the expected task and deletes the line in the database.

This way, you can actually do a scheduled/timeout pattern but becareful the pitfals of having too many tasks or running long scripts in the background. It can easily kill your server.
lovelf
Forum Contributor
Posts: 153
Joined: Wed Nov 05, 2008 12:06 am

Re: Script that auto executes.

Post by lovelf »

Got it.

Thanks.
Post Reply