[SOLVED] header("Location: .....") Question

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
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

header("Location: .....") Question

Post by dashifen »

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
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

Post by dashifen »

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
User avatar
m3mn0n
PHP Evangelist
Posts: 3548
Joined: Tue Aug 13, 2002 3:35 pm
Location: Calgary, Canada

Post by m3mn0n »

[php_man]die()[/php_man] or [php_man]exit()[/php_man]

8)
The Monkey
Forum Contributor
Posts: 168
Joined: Tue Mar 09, 2004 9:05 am
Location: Arkansas, USA

Post by The Monkey »

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...

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();
?>
You may have already known this, but you sounded a bit confused with your theory, and I just wanted to make sure.
dashifen
Forum Commoner
Posts: 35
Joined: Thu Feb 05, 2004 9:53 pm
Location: Champaign - Urbana, IL

Post by dashifen »

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...
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.
Post Reply