if want a single PHP file to reirect to different pages according to different conditions say depending on the outcome of a post..
if(condition 1) {
header("Location: page1.php");
exit;
}
else if(condition 2) {
header("Location: page2.php");
exit;
}
else {
header("Location: page3.php");
exit;
}
Aboslutely!
You might want to make sure the headers haven't been sent first. You can use headers_sent
for this
if (!headers_sent()) {
// ...
}
A good way to handle this might be
function redirect_to($location) {
if (headers_sent($filename, $line)) {
trigger_error("Headers already sent in {$filename} on line {$line}", E_USER_ERROR);
}
header("Location: {$location}");
exit;
}
// you can now use it like this
if(condition 1) {
redirect_to("page1.php");
}
else if(condition 2) {
redirect_to("page2.php");
}
else {
redirect_to("page3.php");
}
Assuming that your condition N
if
statements are valid, there's nothing wrong with having different headers for different cases.
Yes, as long as nothing was outputted before any header() call.
Yes that should work provided you don't echo anything onto the page before calling that function. You can have as much PHP as you like as long as you don't print anything out upstream of the header function.