0

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

 <!doctype html>
 <html lang="en">
 <head>
    <meta charset="UTF-8">
    <title>Document</title>

 </head>
 <body>
    hello
    <?php echo 'hello'; ?>
    <?php header('Location: http://www.google.com/'); ?>
 </body>
 </html>

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 ?

7
  • 4
    Did you restart your server after editing php.ini? Commented Oct 16, 2014 at 9:50
  • possible duplicate of PHP header location-redirect doesn't work - why? Commented Oct 16, 2014 at 9:51
  • I didn't edit anything, it was there this way always Commented Oct 16, 2014 at 9:59
  • 1
    @CyrilBeeckman the duplicate answer is asking about why not redirecting. My question is about why redirecting? Commented Oct 16, 2014 at 10:00
  • Oh sorry, are you in local ? Commented Oct 16, 2014 at 10:08

1 Answer 1

2
 <?php ob_end_flush(); 
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.

Sign up to request clarification or add additional context in comments.

3 Comments

The configuration of output_buffer in ini file is there for always, i did not edit it.
Are you using WAMP in local?
The default for XAMPP appears to be output_buffering=4096. The "settings" quoted in the question are from the "Quick Reference" section further up the file - they aren't the actual settings, which appear later in the file (as mentioned above). See also stackoverflow.com/questions/2425806/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.