You could use a function similar to:
function redirect($url) {
ob_start();
header('Location: '.$url);
ob_end_flush();
die();
}
Worth noting, you should always use either ob_flush()
or ob_start()
at the beginning of your header('location: ...');
functions, and you should always follow them with a die()
or exit()
function to prevent further code execution.
Here's a more detailed guide than any of the other answers have mentioned: http://www.exchangecore.com/blog/how-redirect-using-php/
This guide includes reasons for using die()
/ exit()
functions in your redirects, as well as when to use ob_flush()
vs ob_start()
, and some potential errors that the others answers have left out at this point.