I've noticed on my page that when I use header("Location: ....") to redirect the visitor to another site, the original page seems to continue executing. Is this supposed to happen? If so, is there a way to halt execution after the redirection header is sent?
For example, on my site there is a regular expression that passwords must match to be considered "secure" for the purposes of the site. If a password chosen does not match that regular expression, then the visitor is redirected back to the form so they can enter a new password. However, I noticed that the erroneous password still gets saved in the database. When the user makes a new, "secure" password the erroneous one is replaced with the good one. However, if the user decides to stop after getting an error thinking that they'll just keep their old password, they actually have the erroneous one.
I can't figure out what's going on unless the header() function does not halt execution when the "Location: ...." header is sent.
Also, my trusty O'Reilly's Programming PHP manual states that you shoul use an absolute path to the file you redirect to. It's been working fine for me with relative paths (except for the problem detailed above). Could this be the reason for the above problem? If not, what's the point of coding the absolute path into the header("Location: ...") function calls?
Thanks a bunch,
David Goldfeder
Web and Database Specialist
University of Illinois, Urbana-Champaign
Department of Mechanical and Industrial Engineering
[SOLVED] header("Location: .....") Question
Moderator: General Moderators
Nevermind. I think I answered my own problem, but confirmation would be handy. By putting an exit statement after each header() function call, it looks like the problem is solved. Is this the accepted method?
Also, any answers to my question above regarding absolute vs. relative paths in the header() function would be handy.
Thanks all!
David Goldfeder
Web and Database Specialist
University of Illinois, Urbana-Champaign
Department of Mechanical and Industrial Engineering
Also, any answers to my question above regarding absolute vs. relative paths in the header() function would be handy.
Thanks all!
David Goldfeder
Web and Database Specialist
University of Illinois, Urbana-Champaign
Department of Mechanical and Industrial Engineering
-
The Monkey
- Forum Contributor
- Posts: 168
- Joined: Tue Mar 09, 2004 9:05 am
- Location: Arkansas, USA
Note that you only want an exit() function after calling a header(Location), and only then if you have already sent all of the other headers. You don't need exit() after each header...
You may have already known this, but you sounded a bit confused with your theory, and I just wanted to make sure.
Code: Select all
<?php
//incorrect:
header("HTTP/1.0 404 Not Found");
exit();
header("Cache-Control: no-store, no-cache, must-revalidate");
exit();
//correct:
header("HTTP/1.0 404 Not Found");
header("Cache-Control: no-store, no-cache, must-revalidate");
header("Location: http://www.somecoolsite.com");
exit();
?>Sure, that's what I did. So far the only other time I used the header() function was for security images (you know those one with random text in them to help avoid bots that sign up for your site) so I have an exit call after about 99% of my header() calls. Thanks for clarifying, that would have been important.The Monkey wrote:Note that you only want an exit() function after calling a header(Location), and only then if you have already sent all of the other headers. You don't need exit() after each header...