tags:

views:

57

answers:

2

Going through the php.net site, it had an example for header, which says would give me error. I copied it, and executed on on WAMP, but it didn't showed me any error, but did redirect to the site.

<html>
<?php
/* This should give an error (but it doesn't!). Note the output
 * above, which is before the header() call */
header('Location: http://www.example.com/');
?>

Just wanted to know, if its a right behavior on my WAMP, or its an error, or if I have any particular settings active in php.ini file which is making this work!!!. Let me know if anyone needs my php.ini to be copied here!!

Thanks, Tanmay

+3  A: 

It sounds like you have output_buffering enabled.

http://php.net/manual/en/outcontrol.configuration.php

Standard configuration would be to error because data has already been output, and headers need to come first. Output buffering would allow headers to appear in code after other output, but it would still output the headers first due to the buffer.

Fosco
@Fosco: I did check my php.ini file, which had output buffering on.Just on curiosity part, if I had 3 different header ('Location:....') comand, it wont execute the first two, and will be redirected to the 3rd site!?
jtanmay
Thanks for your answer this quick :)
jtanmay
@jtanmay: while that is an interesting question, it's not something that should ever happen... you can only be redirected to a single location, so 'good code' would never create more than one location header.
Fosco
A: 

Headers are sent as soon as any text is sent to the browser, and can only be sent once. once the is sent, headers are sent along with it, so trying the header function after that wll throw a headers already sent error.

GSto
The OP is asking why it didn't throw an error like they expected.
Blair McMillan