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!
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?
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.