zander1983 Posted May 18, 2011 Share Posted May 18, 2011 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! Quote Link to comment https://forums.phpfreaks.com/topic/236792-php-code-executes-after-a-header-redirect/ Share on other sites More sharing options...
PFMaBiSmAd Posted May 18, 2011 Share Posted May 18, 2011 The only thing a header() statement does is send a raw http header to the browser - Description void header ( string $string [, bool $replace = true [, int $http_response_code ]] ) header() is used to send a raw HTTP header. See the » HTTP/1.1 specification for more information on HTTP headers. If you want your php code to stop executing, you must put an exit; statement after the header() statement. Quote Link to comment https://forums.phpfreaks.com/topic/236792-php-code-executes-after-a-header-redirect/#findComment-1217260 Share on other sites More sharing options...
zander1983 Posted May 18, 2011 Author Share Posted May 18, 2011 that worked, thanks. Ive been using header(location) for years without knowing this! thanks! Quote Link to comment https://forums.phpfreaks.com/topic/236792-php-code-executes-after-a-header-redirect/#findComment-1217262 Share on other sites More sharing options...
PFMaBiSmAd Posted May 18, 2011 Share Posted May 18, 2011 A lot of people don't put exit; statements after header() redirects in their login check code and bot scripts can access the 'protected' pages the same as if the security check code isn't even present. Quote Link to comment https://forums.phpfreaks.com/topic/236792-php-code-executes-after-a-header-redirect/#findComment-1217265 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.