1

I have a PHP script that needs to process some more information in the background after returning the response.

I made it work by following this answer as well as turning off FastCGI Output Buffer:

<IfModule mod_fcgid.c>
FcgidOutputBufferSize 0
</IfModule>

It won't work without turning off FastCGI Output Buffer.

However, I only have one script that needs this. It would be nice if I can keep FastCGI Output Buffer for all other scripts.

Is it possible to just make one PHP script ignore FastCGI Output Buffer?

Community
  • 1
  • 1
Shawn
  • 2,675
  • 3
  • 25
  • 48
  • 1
    It sounds like you need to run a task in the background, not futz around with output buffering. – Sammitch Aug 18 '16 at 00:59
  • But the user needs to initiate it with data. – Shawn Aug 18 '16 at 01:11
  • 1
    Yeah so whatever function this is takes its parameters, performs its immediate tasks [eg: content generation], starts the background task, and returns. The page is delivered as expected without having to selectively disable output buffering, and whatever long-running thing you started is still going in the background. – Sammitch Aug 18 '16 at 01:16
  • Yes it's exactly what I need. Please add an answer with some code example. Like I said, the script I took from the other post's answer won't work without turning off FastCGI Output Buffer. The user gets the response after the long-running task finishes. – Shawn Aug 18 '16 at 05:37
  • In that specific case, you could just prefill the (default) buffer, e.g. `echo str_repeat(' ', 65537);` – Simon Jul 16 '20 at 09:49

1 Answers1

1

Maybe you can use nested Apache directives to check the request URI. Like this:

<IfModule mod_fcgid.c>
    <If "%{REQUEST_URI} == '/path/to/script.php'">
        FcgidOutputBufferSize 0
    </If>
</IfModule>
Cave Johnson
  • 6,499
  • 5
  • 38
  • 57
  • What if I only need this for a specific function in the script? – Shawn Aug 18 '16 at 00:29
  • @Shawn Sorry I don't know how to do that. Hopefully someone else who knows can answer. – Cave Johnson Aug 18 '16 at 00:46
  • cPanel keeps throwing me syntax error with your solution. Any idea? – Shawn Sep 11 '16 at 19:48
  • @Shawn I'm not sure why there is a syntax error. I used this page as a reference: https://httpd.apache.org/docs/current/expr.html#examples. It looks like it should work. The error doesn't give you any more detail? – Cave Johnson Sep 13 '16 at 23:30