Page 1 of 1

everything executes after a header location!?

Posted: Wed May 18, 2011 5:04 pm
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!

Re: everything executes after a header location!?

Posted: Wed May 18, 2011 5:10 pm
by flying_circus
Best practice is to put an exit(); after a header(location:) to prevent further script execution.

Re: everything executes after a header location!?

Posted: Wed May 18, 2011 5:13 pm
by Zander1983
that worked, thanks. Ive been using header(location) for years without knowing this! thanks!

Re: everything executes after a header location!?

Posted: Fri May 20, 2011 10:18 am
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.