everything executes after a header location!?

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
Zander1983
Forum Newbie
Posts: 20
Joined: Mon Mar 21, 2011 2:26 pm

everything executes after a header location!?

Post by Zander1983 »

Hi
I've been working on a site for 6 months and have used the header command to redirect loads and loads of times. But i just noticed some crazy behaviour today. Heres a test page:

<?php
include("includes/db_connect.php");

header('location:stores.php');


$sql = "insert into test (id) values (77)";
$result = mysql_query($sql);
?>

when i hit this page, i get redirected to stores.php, but the sql gets executed! Is this supposed to be the behaviour? Have i completely missed the point of header(location:)?? Im baffled as i've used this before, but only now noticed this crazy behaviour!
User avatar
flying_circus
Forum Regular
Posts: 732
Joined: Wed Mar 05, 2008 10:23 pm
Location: Sunriver, OR

Re: everything executes after a header location!?

Post by flying_circus »

Best practice is to put an exit(); after a header(location:) to prevent further script execution.
Zander1983
Forum Newbie
Posts: 20
Joined: Mon Mar 21, 2011 2:26 pm

Re: everything executes after a header location!?

Post by Zander1983 »

that worked, thanks. Ive been using header(location) for years without knowing this! thanks!
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: everything executes after a header location!?

Post by Weirdan »

Zander1983 wrote:Is this supposed to be the behaviour? Have i completely missed the point of header(location:)??
Answer to both is yes.
Post Reply