Saturday, March 23, 2019

Why isn't header() function in php throwing an error?



I am putting the header function after some output but I'm still being sent to the location specified.



 



Document




hello







Settings of output_buffering in php.ini are all commented out



; output_buffering
; Default Value: Off
; Development Value: 4096
; Production Value: 4096


Why am I still being redirected to google.com ?


Answer




 echo 'hello';
header('Location: http://www.google.com/'); ?>


You may have had output buffering on on the server: output buffering will not output anything until the script finishes running. As you mentioned that you did OFF, so you need to restart server again. That allows it to get the header out before the actual output (as it knows the headers should be sent first).



If that makes sense.
Edit



; Output buffering is a mechanism for controlling how much output data

; (excluding headers and cookies) PHP should keep internally before pushing that
; data to the client. If your application's output exceeds this setting, PHP
; will send that data in chunks of roughly the size you specify.
; Turning on this setting and managing its maximum buffer size can yield some
; interesting side-effects depending on your application and web server.
; You may be able to send headers and cookies after you've already sent output
; through print or echo. You also may see performance benefits if your server is
; emitting less packets due to buffered output versus PHP streaming the output
; as it gets it. On production servers, 4096 bytes is a good setting for performance
; reasons.

; Note: Output buffering can also be controlled via Output Buffering Control
; functions.
; Possible Values:
; On = Enabled and buffer is unlimited. (Use with caution)
; Off = Disabled
; Integer = Enables the buffer and sets its maximum size in bytes.
; Note: This directive is hardcoded to Off for the CLI SAPI
; Default Value: Off
; Development Value: 4096
; Production Value: 4096

; http://php.net/output-buffering
output_buffering = On


In php.ini you'll find as above.. Find keyword 'output_buffering' throughout the php.ini file, It should be on somewhere.. To check it in other way, use phpinfo(); in any php page and run that page you'll find output_buffering as '1' means it's On.. So by doing it Off you can stop it for lifetime or for temporary period, you can use ob_end_flush(); as I mentioned earlier.


No comments:

Post a Comment

plot explanation - Why did Peaches' mom hang on the tree? - Movies & TV

In the middle of the movie Ice Age: Continental Drift Peaches' mom asked Peaches to go to sleep. Then, she hung on the tree. This parti...