Reputation: 3814
I thought you couldn't redirect using header('location:..
in php if you had already sent some output. However, this test program works as (un)expected with the local server.
$ cat > index.php
Hello,
<?php
if ($_SERVER['REQUEST_URI'] !== "/") {
header('location: /');
die();
}
?>
World!
and then I start the php local server like this
$ php -S localhost:8000
and then when I go to http://localhost:8000/index.php?test=1
it redirects me to http://localhost:8000
.
Why isn't it complaining that I have already sent output? is it something different about php 7? Something different about modern browsers? Something different about the demo server? Or do I need to do something slightly different to get the error I got last time I used php?
Upvotes: 1
Views: 39
Reputation: 2555
I tried your script, and for me it reported back the expected "Cannot modify header" warning and only displayed "Hello,".
The main thing that could be different for you is the output_buffering
variable in your php.ini - if it is not disabled, then it automatically buffers the output up to a certain size. So check if it is set to either "On" or something like 4096. The default value if Off, but there are a lot of sample configs where it is enabled.
Upvotes: 1