Just as the title says, Wondering if I need to put the exit();
after each PHP if statement, or just the main if statement or just the last if statement? Currently I have it on the last if statement and it works fine.
if ( isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']) ) {
if ( strpos($_SERVER['QUERY_STRING'], 'occupation') === false || $_SERVER['REQUEST_URI'] != strtolower($_SERVER['REQUEST_URI']) || $occupation == '' ) {
header ('Location: http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position), true, 301);
}
if ( $arr_position == '' ) {
header ('Location: http://' . $domain . '/jobs/', true, 301);
}
if ( isset($home) && $arr_position == '' ) {
header ('Location: http://' . $domain, true, 301);
}
if ( isset($home) && $arr_position != '' ) {
header ('Location: http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position), true, 301);
exit();
}
}
UPDATE
I changed it to have an exit() after each header location call and all is still working fine. This is in my main template file, so yes there is code below that needs execution if nothing matches in the header location if block.
I now have it like this
if ( isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']) ) {
if ( strpos($_SERVER['QUERY_STRING'], 'occupation') === false || $_SERVER['REQUEST_URI'] != strtolower($_SERVER['REQUEST_URI']) || $occupation != strtolower($arr_occupation) ) {
header ('Location: http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position), true, 301);
exit();
}
if ( $arr_position == '' ) {
header ('Location: http://' . $domain . '/jobs/', true, 301);
exit();
}
if ( isset($home) && $arr_position == '' ) {
header ('Location: http://' . $domain, true, 301);
exit();
}
if ( isset($home) && $position == urlencode($arr_position) ) {
header ('Location: http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position), true, 301);
exit();
}
}
UPDATE 2
I went ahead and used DefiniteIntegral answer because I like the logic, but I had to use separate if statements within the main block. Using elseif
does not work. I have fully tested it and using different conditions in each elseif
and it just won't work right. So I am fine with using separate if statements within the main block.
Here is how it looks now
if ( isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']) ) {
$new_url = false;
if ( strpos($_SERVER['QUERY_STRING'], 'occupation') === false || $_SERVER['REQUEST_URI'] != strtolower($_SERVER['REQUEST_URI']) || $occupation != strtolower($arr_occupation) ) {
$new_url = 'http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position);
}
if ( $arr_position == '' ) {
$new_url = 'http://' . $domain . '/jobs/';
}
if ( isset($home) && $arr_position == '' ) {
$new_url = 'http://' . $domain;
}
if ( isset($home) && $arr_position != '' ) {
$new_url = 'http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position);
}
if ($new_url !== false) {
header('Location: ' . $new_url, true, 301);
exit();
}
}
FINAL UPDATE
Finally got it working right with the elseif
statements. Had to add more conditions to the if
statements in each elseif
block of code.
Final code looks like this
if ( isset($_SERVER['QUERY_STRING']) && !empty($_SERVER['QUERY_STRING']) ) {
$new_url = false;
if ( !isset($occupation) || $occupation != strtolower($arr_occupation) || $_SERVER['REQUEST_URI'] != strtolower($_SERVER['REQUEST_URI']) ) {
$new_url = 'http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position);
} elseif ( isset($home) ) {
if ( !isset($position) || $position != $arr_position || $position == '' ) {
$new_url = 'http://' . $domain;
} else {
$new_url = 'http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position);
}
} elseif ( $currentPage == 'Jobs' ) {
if ( !isset($position) || $position == '' ) {
$new_url = 'http://' . $domain . '/jobs/';
} elseif (isset($position) && $position != '' && $position != $arr_position) {
$new_url = 'http://' . $domain . strtolower ( '/jobs/?occupation='.$arr_occupation.'&position='.$arr_position);
}
}
if ($new_url !== false) {
header('Location: ' . $new_url, true, 301);
exit();
}
}