Ignore_user_abort and redirect to php?

I have a very similar setup for a person here:
PHP background processes
those. A very long script that takes up to 10 minutes. However, I need a person who calls the script redirected to the main page while the script is running. In other words, I need the user interface to be something like this:

  • press refresh button
  • script starts to run, may take up to 10 minutes
  • the user is immediately redirected to the home page.

Can I use only PHP? I suppose I need

ignore_user_abort(true); set_time_limit(0); 

But how to redirect the user? I cannot use javascript because the output is only written to the page in large increments, and I want the redirect to be immediate. Can I use headers? Or will it be with things?

Alternatively, I could use the cron approach, but I have no experience in creating a cron job or when running php code (is this possible?)

Thanks,
Mala

Update:
Using headers for redirection does not work - the page will not load until the script is executed. However, in the end, the web server shuts down and says "Zero response: the requested URL cannot be restored" (although the script continues to work). I guess my only option is to go with the idea of โ€‹โ€‹working cron. Ik!

+4
source share
4 answers

I tried several methods and none of them work, I even tried to use register_shutdown_function() , but that also failed. I think you are stuck in creating a cron job.

I just remembered something (but I have not tested it), you can try to do something like this:

 set_time_limit(0); ignore_user_abort(true); ob_start(); // not sure if this is needed // meta refresh or javascript redirect ob_flush(); // not sure if this is needed flush(); // code to process here exit(); 

Not sure if this will work, but you can try.

+1
source

The most obvious solution for me would be to separate the forwarding and background computing in two separate files, and let the redirect script execute a 10 minute script:

job.php:

 <?php // do the nasty calculation here 

redirect.php:

 <?php // start the script and redirect output of the script to nirvana, so that it // runs in the background exec ('php /path/to/your/script/job.php >> /dev/null 2>&1 &'); // now the redirect header ('Location /index.php'); 

Assumptions for this: you must be on a Linux host with safe_mode disabled or set safe_mode_exec_dir accordingly. When you work under windows, the exec string must be adapted, and the rest of safe_mode remains true.

Note. When you need to pass arguments to a script, use escapeshellarg() before passing it, see also the PHP manual for exec

+2
source

I have a similar situation with login processing. To keep it short ... I get a PDT, IPN, and everyone sends me an email. The letter is sent to the client via IPN VERIFIED to provide the serial number and password to the client. Since PDT and IPN I use goto to send me an electronic journal, not a bunch of consecutive ifs. After reading a lot of answers, I studied each to understand what would suit my Issu. I finally used ...

 <?php ignore_user_abort(TRUE); // at very top 

Since I worked with progressive checks (no ifs), if they were unsuccessful, I use for example ...

  $mcalmsg .= "Check [serialnbr]\r\n"; if (empty($_POST['serialnbr'])) { header('Location: '.$returnurl.'?error=1'); $mcalmsg .= "Missing [serialnbr]\r\n"; goto mcal_email; // Last process at end of script } else {$serialnbr=strtoupper(htmlspecialchars(trim($_POST['serialnbr']))); $mcalmsg .= "[serialnbr]=$serialnbr\r\n"; } 

This (for now) works just as necessary. Of course, the script has more, but follows the same concept. Where this location is indicated, there are also 3 information pages, each of which can be dropped using the same concept.

 mcal_email: //last process before ending, always gets here after all else from goto or clearing all checks. // compose email and send ?> // end of script 
0
source

Why not try the header approach and see what happens? You can also try calling the php header method and see if this does the trick. I would work on trial and error to understand what would solve your problem.

-one
source

All Articles