Page 1 of 1

How To Use the Header() function

Posted: Thu Oct 26, 2006 3:25 am
by abrogard
I'm a raw beginner.

I put together, from two other scripts, a script to email the input from a form and to write it to a mysql database at the same time.

The original email scripts had three or more little 'if' routines to catch errors, upon which there would be a jump to a new page and an exit. The jump via this header() function.

When I stuck the two together I started to get this 'can't modify header information...' error.

Reading up on it I found it is because the script has already made some output before the header() func, being 'print' statements on the success of the sql write.

I commented out the print statements and everything is fine.

But it raises the question for me about how to do this thing - jump to another page out of an 'if' ?

It seems to me to be quite reasonable to write a script that does all sorts of things and prints out a sort of 'progress report' as it is going and then, eventually, decides to leap to another page because of something it has encountered, good or bad.

But header() won't do this, it seems.

So what will?

Or has that methodology got to be abandoned?

Does this also mean that you can't use header() twice even when you use it correctly? That is: you use header() before any output, then make the output and therefore can't use header() again?

regards,

ab :)

Posted: Thu Oct 26, 2006 3:58 am
by s.dot
You can use header twice.. for two headers.

There are many uses for the header() function other than redirecting to another page. If that's the case, than there can be NO output prior to the header() call. Think about it. If you're sending output, then headers were already sent with that output!

With that said, only one header() redirection per page. (why need more?) :P

If you need to output data prior to calling header (most cases you don't need to, but some browsers won't accept header() redirection), then use the meta refresh tag in the head of your HTML document. ;)