How To Use the Header() function

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
abrogard
Forum Newbie
Posts: 11
Joined: Sun Apr 04, 2004 6:02 am

How To Use the Header() function

Post 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 :)
User avatar
s.dot
Tranquility In Moderation
Posts: 5001
Joined: Sun Feb 06, 2005 7:18 pm
Location: Indiana

Post 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. ;)
Set Search Time - A google chrome extension. When you search only results from the past year (or set time period) are displayed. Helps tremendously when using new technologies to avoid outdated results.
Post Reply