Here's how header() works
You can put header("Location: whateverpage.php"); anywhere you want. Top/bottom of script doesnt matter. What is important is to put exit(); right after. The reason being that all your doing with the header call is changing the headers, the script wants to finnish before it uses those new headers. So if you stop the script then you will redirect the page.
Also, if you tried echoing or printing anything to the screen before header(), then it wont work because you're basiclly trying to say you want a page returned to the user yet you also want to go to another page. When you echo, you are saying that the current heading is what you want. So thats why when you change the heading later it says "Cant change header"
-B
bwest
Junior Poster in Training
57 posts since Jul 2004
Reputation Points: 14
Solved Threads: 1
like sarahk said, as soon as their is some http output, you cannot modify the headers. This is a restriction of the http protocol.
PHP can also buffer http output and only send the output when you want it to using the ob_start() and ob_end_flush().
This is also useful in many other situations, but especially in yours where you want to modify the http header information anywhere withing the script.
so if you call ob_start(); on the first line of your executing php file, then whenever you want to make a header redirect, you just call header() then exit();.
note: I believe exit will automatically flush the http headers. If not then you have to call ob_end_flush(); before exit.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101
Hello all here on DW, and even though I've popped in here a couple of times...this is my first post.
I also do know want you to think I am "hijacking a thread", but I also am having a problem that is beating me up.
Instead of creating a new thread, like most others like..I personally feel that keeping a subject "on a direct topic" is best, as searching thru a forum...for a subject with the same topic...and reading them all, well.. I feel redundancy.
I will be more than happy to if required though.
My problem
I am also getting the error, in a normal situation doing "page by page", it isn't a problem, but I am trying to redirect after a "form submit" that is placed globally in the left pane of the site. (ie...displayed on all pages of the site, and pulled with what the original programmer used as "left.php"
The form it self, is submitted then into /js/ajax_form.php in which I have tried all the above...and still get this error.
file "left.php" (in which is all html) and I place it...no errors, and does nothing.
I am really lost on this one, and am thinking I am going to have to place it globally?
Thx for taking the time to read this,
T~
Hi T,
The headers mentiond here are HTTP Headers. I think you got it confused with the HTML HEAD tags. (This is often confused).
The HTTP Header is "Location: http://example.com/" where "http://example.com/" is the URL you want to redirect to.
So if you want to redirect with HTTP from PHP you do:
header('Location: http://example.com/');
exit();
As mentiond above, you can also use output buffering so that you can have a header() call after you output some HTML or what not.
digital-ether
Nearly a Posting Virtuoso
1,293 posts since Sep 2005
Reputation Points: 461
Solved Threads: 101