像没有浏览器的应用程序一样运行PHP脚本

Hi I am new to PHP and have no idea if what I am about to ask is even possible or does it even make sense but here goes.

I want to execute a PHP script as if I am executing a standalone application on the WebServer, what I am trying to implement is that when the Customer purchases something on the website and the once he sees the payment confirmation notice on the website, he should be allowed to close the browser window or logoff without affecting the big order generation process that get's started once the user is taken to the page that displays that the payment that he made was successful.

Right now I am making use of AJAX to call my after payment processing PHP script and have kept that script to ignore any user abort call.

This is the page that tells the user that the payment was received successfully.

thankyou.php

This is the page that performs the processing that needs to be done only after successful receipt of payment

FinishCheckoutProcess.inc.php

Now thankyou.php makes use of AJAX to execute FinishCheckoutProcess.inc.php asynchronously and FinishCheckoutProcess.inc.php has a PHP.ini setting in it that goes like this: ignore_user_abort(true);

Now the combination of AJAX and ignore_user_abort(true) allows the after payment process to run without any errors even if the user closes his browser window, but since this script has nothing to do with the user or the browser I just wanted to know if it is possible to run this script in the background like a standalone application independent of the browser.

Also my WebServer is Apache and OS is Linux(Ubuntu OS).

My work is getting done but I just want to know if there is a better/safer way to do it.

Anyway thanks in advance to everyone, this site has helped me more than any book could have. So all you experts out there who donate their times to newbies like me you guys are awesome. Please keep up the good work.

Again thanks a lot.


Based on suggestions received If I use the "exec" method to execute the FinishCheckoutProcess.inc.php, will this execute database related commands and will it be able to run further PHP scripts.

FinishCheckoutProcess.inc.php in turn executes a series of other PHP scripts which in turn executes other PHP scripts, so will using "exec" command to run FinishCheckoutProcess.inc.php create any difficulties.

FinishCheckoutProcess.inc.php process also does interaction with the MySQL database, so will I be able to do this if I execute this script using "exec" command. I am passing the necessary MySQLi connection object to this PHP script right now. So can I pass it the same way to it using "exec"

Also the process is quite heavy as it generates a set of 4 image files using IMagick and ImageMagick.

It generates a set of 4 image files for every product ordered, so if the quantity of 1 product is 10 then the total files generated will be 1x10x4 = 40

If there are two products with one having quantity as 2 and the other having quantity as 4 then the total files generated will be 1x2x4 = 8 + 1x4x4 = 16 = 24

So this script might need to run for a long time and cannot be allowed to be stopped due to time out reasons, it needs to finish what it started.

Basiclly the FinishCheckoutProcess.inc.php logic and process is quite complex so just want to confirm if the "exec" can handle it or not.

Also I am not sure but some of them also make use of $_SESSION variables, but if this a problem I can modify it, $_SESSION variables only get's used in one place and yes the $_SESSION get's set in the browser before the FinishCheckoutProcess.inc.php script is executed. By some previous PHP script.

I just want to execute the FinishCheckoutProcess.inc.php script independent of the parent/calling script i.e. thankyou.php, so that if the user closes the browser then the FinishCheckoutProcess.inc.php will not stop or abort becuse the parent/calling script i.e. thankyou.php is now no longer running.

FYI you can run php scripts like php my/script.php.

A safer way to do it would be have a master/worker process workflow. The master process runs on the server and checks a queue of work and the spawns worker processes to handle items on the queue as the arrive.

In your scenario you add stuff to the queue when the user pays. Once it is added to the queue you can send back thankyou.php to the user and they can continue or leave or whatever. Once the work is on the queue your master process spawns a worker process to handle the stuff (basically does everything in FinishCheckoutProcess.inc.php).

You can implement this in php with: php master.php
master.php

while( true ){
    //check queue
    //if found queue item
       //shell_exec( 'php worker.php' );
}

From what i understand, you are looking for something like Laravel offers with it's illuminate/queue package:

Queues allow you to defer the processing of a time consuming task, such as sending an e-mail, until a later time which drastically speeds up web requests to your application.

This isn't something that only Laravel offers, though it does simplify/ease the implementation of such mechanism.

In the background you have supervisord executing a "worker" php script that executes tasks you put in a common place (db tabel, filesystem, anything), those tasks are usually references to a certain class/method with some variables to send to it.

The following links might give you a better understanding:

There are many ways you could implement a queue system, also without the use of supervisord. But i recently implemented this method myself because it guarantees my tasks are being processed, even after server restart (if configured properly).