Sowmya
Sowmya

Reputation: 43

How to execute a PHP file in background for every 30 seconds

I am working on flight portal.

If any user book a ticket he/she will get some status like BOOKING SUCCESS / BOOKING PENDING.

If the status is BOOKING SUCCESS the user will get a mail with the ticket details.

In case of BOOKING PENDING a message ( your ticket is in process, you receive a message/email after the ticket confirmed) will be shown to the User.

Now from the backend i have to run a PHP file for every 30 seconds Until i get the status Success.

This will be a continuous process.

Can anyone tell me with a good example.

I will be thankful,

Sowmya

Upvotes: 1

Views: 4885

Answers (3)

MisterBla
MisterBla

Reputation: 2385

There are multiple ways of doing so.

The best way would be using a using a Cron Job.

Other ways are using a webworker or a an AJAX request, but those require a browser to be active at all times.

Here you can find how to set it up.

As for a webworker, it's an HTML 5 feature so it wouldn't be adviced if you expect people using browsers that don't support it.

If your server runs on cPanel, you can easily set up a cron job. Otherwise you'd have to do it through your terminal I think or through your hosting provider if you have paid hosting without cPanel.

You can set up the cron job like this, the command used in the bar is
/path/to/php -q /absolute/path/to/your/phpfile.php >/dev/null

Upvotes: 2

Omar Faruk Sharif
Omar Faruk Sharif

Reputation: 581

If you want to just reload your page you can use Javascript to achieve this.

Plain JavaScript

setTimeout(function() {
     window.location.reload();
},  30000);

jQuery

$(function() {
    setTimeout(function() { 
        window.location.reload();
    },  30000);
});

Otherwise, you can use CURL. Visit here to get to know about usage of CURL.

Upvotes: 1

Vimalnath
Vimalnath

Reputation: 6463

You are looking for CRON, this helps you to schedule your task at your scheduled time. That is your could execute/run your PHP script at the time scheduled by you.

* * * * * /path/to/php_file
* * * * * sleep 15; /path/to/php_file
* * * * * sleep 30; /path/to/php_file
* * * * * sleep 45; /path/to/php_file

The above cron job will run your php script for every 15 seconds. Here sleep will help to delay the script for every 15 seconds.

Upvotes: 18

Related Questions