subhash
subhash

Reputation: 39

php redirect immediately and also continue execution which dont send data

I need to execute a bunch of functions that consume nearly 50 seconds to complete. But i want redirect to page after execution few function itself and also continue further execution.

The below function execute on form submit as in process.php // it wont echo anything

// all function in serprate process page.
func1
func2
func3
// After above 3 function i need to php redirect to redirect now immediately example.com
func4
func5
func6

I tried using php header but it won't immediately redirecting.

header("Location:http://www.example.com");

If I use exit(); it will redirect but not process function 4,5 and 6.

what i need is any way to redirect immediately after first 3 functions and continue execution.

Upvotes: 0

Views: 512

Answers (2)

Denis de Bernardy
Denis de Bernardy

Reputation: 78523

On paper, you can achieve this by combining flush() and ignore_user_abort():

ignore_user_abort(true);
do_stuff();
send_redirect();
flush();
do_more_stuff();

Manual pages:

Note the known caveats: some browsers (old IEs in particular, and possibly new ones) want a minimum amount of bytes received before processing what you send them, so you might end up needing to toss in some long string in an html comment for it to work as expected.

In practice, the more conventional approach is to register a cron job task in some table, and have a cron.php file take care of pending tasks in a completely separate (and independent) request.

A less conventional approach is also highlighted in the comments: issue a shell command or something to that order — be very wary of sanitizing input if you do that.


Adding this for reference (see comments below):

<!-- IE bug fix: pad the page with enough characters such that it is greater than 512 bytes, even after gzip compression abcdefghijklmnopqrstuvwxyz1234567890aabbccddeeffgghhiijjkkllmmnnooppqqrrssttuuvvwwxxyyzz11223344556677889900abacbcbdcdcededfefegfgfhghgihihjijikjkjlklkmlmlnmnmononpopoqpqprqrqsrsrtstsubcbcdcdedefefgfabcadefbghicjkldmnoepqrfstugvwxhyz1i234j567k890laabmbccnddeoeffpgghqhiirjjksklltmmnunoovppqwqrrxsstytuuzvvw0wxx1yyz2z113223434455666777889890091abc2def3ghi4jkl5mno6pqr7stu8vwx9yz11aab2bcc3dd4ee5ff6gg7hh8ii9j0jk1kl2lmm3nnoo4p5pq6qrr7ss8tt9uuvv0wwx1x2yyzz13aba4cbcb5dcdc6dedfef8egf9gfh0ghg1ihi2hji3jik4jkj5lkl6kml7mln8mnm9ono
—>

References:

(Actually applies only for http errors, after re-going through it.)

Upvotes: 3

Brewal
Brewal

Reputation: 8189

Try to split your process.php in two files : process.php and process_more.php (i.e.). Assuming your are under a unix os server :

Code for process.php :

// code to execute before redirect
func1();
func2();
func3();
// code to execute after redirect in background
// you can pass some parameters
$command = "php -f /path/to/process_more.php param1 param2"
exec($command . " > /dev/null &"); 
header('Location: http://dn.tld/');
exit();

See more to use parameters with $argv variable, and be careful to prevent the user for injecting another command within the parameters

Code for process_more.php :

func2();
func3();
func4();

Not that you won't be able to access $_GET or $_POST variables. You need to pass any variable you want within the call command.

Upvotes: 2

Related Questions