Header() function not working


I’ve got a problem with a header function using Location.
I tracked it down to, that echoing content before header doesn’t work properly.
Does the order of code causes this problem, or the order of execution?

I have seen ob_start() suggested
but I got this understanding that it is bad form.

Is it or is it not?

And how to use it properly?

before “if” stmt, check whether the session is set or not.
If session is not set,
put the “header” function


Umm why not you put that piece of code on top of the shop.php file? Isn’t that possible?

[fphp]print_r[/fphp] CAN return the string - there’s a second parameter that was added in 4.3.0 for this exact reason.

$a = array(1,2,3,4); // some test array
$result = print_r($a, true);

Of course your reasoning for using [fphp]ob_start[/fphp] still stands; it just so happens that that particular example is incorrect. :wink:

It makes the PHP process take up extra memory as it has to put every output you generate in memory, and only then flush it when the script is done (or when you call one of the ob_end functions). Without ob_start PHP flushes only 4KB at a time, which is then the maximum amount of memory PHP will use at any given time to buffer output. With ob start the maximum is the total output of your script, which might be a lot more than 4KB.

Well, I hardly ever use it. There are some cases with functions that have no return possibility, like print_r, which you might wrap with buffering like so:

$a = array(1,2,3,4); // some test array
$result = ob_get_clean();

$result now contains the output of the buffer, so instead of actually printing the result of print_r has been saved in the buffer and then requested using ob_get_clean().
Of course print_r is a silly example, but I couldn’t think of a better one :slight_smile:

Well, Here’s the flow of my script:
Generate product list,
Add items to cart , generate list of products in cart, with remove link.

<a href=shop.php?cart=remove&amp;index='.$i.'">Remove</a>

So I don’t see how could I change the flow.

This is the if statement what uses the header function.

$key = (int)$_GET['index'];
unset($_GET['cart'], $_GET['index']);
header('Location: shop.php'); }

Also what are the reasons not to use ob_start() as a permanent fix?
It interests me where is this kind of buffer function used?


If it is really a hurry then you are free to use ob_start(). But because it seems that you already know that you have thrown some output before the use of header function, I would also recommend to change the coding flow in that way in which you don’t output anything before using the header() function.

You can only use the header function when no content has been sent to the browser yet. That includes an enter (or a space, or a tab) at the start of the file, before the PHP open tag!
And, just throwing ob_start() in your code would work, but it’s kind of a quick fix, not really a nice solution.
If you can’t get it to work you can your post here and take it from there.