Alex
Alex

Reputation: 25

Server-side script for checking database in the background

I am currently writing a "forum-like" website for our class, which works like a FAQ, where everyone from our class can post answers. I want to implement a system, where one student (class president) can upload these questions (Question which where frequently asked by the teachers, and are likely to become test-questions) and select which user has to answer which question.

Now I want to write a system that reminds (Email) a user after a certain amount of time that his question hasn't been answered, and that he should answer it. So, I need to check my database every ~30 minutes and see if this "answertime" has run out, and send the user an email.

I don't think I can achieve this with an PHP-Script unless there is a way to always run a script in the background. I also thought about writing an external program to check the database, but I don't like using a program, that could fail without me noticing it.

So I wanted to ask, what's the best way to do this?

Upvotes: 0

Views: 105

Answers (2)

SteAp
SteAp

Reputation: 11999

In case cron isn't available on your virtual host, you might run periodical tasks after the end of normal script execution. Here is how:

Check the details of this PHP feature:

register_shutdown_function()

At the start of a script, which gets called often enough by normal users using their browser, register a shutdown function. Then let the script execute as normal and finally execute

exit;

Due to the exit, the browser assumes the server script to be finished - although the registered shutdown function still executes.

Take care, that the shutdown function terminates immediately, if its last execute isn't older than a predefined amount of minutes.

Needless to mention, that the use of a cron job is favorable over this approach.

Upvotes: 0

Naftali
Naftali

Reputation: 146302

You can set a cron job on the server side to run the php script every x minutes
This is for linux.

For windows you can set a scheduled task to run the script every x minutes

Upvotes: 1

Related Questions