-
header redirects
Folks, I have a (working) PHP file that begins as follows:Now I realize that, as written, I never reach the switch statement, but that's not my question. My question is this: Can the header redirect be selectively chosen using the switch statement (as I tried to do after line three)? That is, how, if at all, can I (omit the first header and die statements to just) redirect to a selected target page using the switch statement? When I try to implement it using the switch statement, I get a page-unavailable error message.
A.
-
Those entities ' shouldn't be there, those need to be literal single quotes. There could be other problems. But to answer the underlying question, yes - a different page header redirect should be able to be sent based upon a value. However there's probably a better way to do that, like setting the page to be rediected to based upon that value and having only one redirect header. Also be aware that you cannot write to the page before invoking the header. Well you might be able to, but usually not. Another thing, a break after die is superfluous.
-
This works "here" (pun intended):
PHP Code:
<?php
$here = isset($_GET['here'])? $_GET['here'] : '';
switch ( $here ) {
case 'index':
header('Location:http://www.MarainLaw.com/page.php?here=sexual-assault-in-new-jersey');
die();
case 'prostitution-lawyer-nj':
header('Location:http://www.marainlaw.com/page.php?here=prostitution');
die();
default:
header('Location:http://www.MarainLaw.com/page.php?here=sexual-assault-in-new-jersey');
die();
}
?>
-
John, two issues exist, one corresponding to each of your much appreciated replies. Anent your first reply, the text that I uploaded did not contain ' characters. They appear to have been converted from apostrophes at some point in the message uploading process. Anent your second reply, I do understand (and heartily approve of) the isset test you provided. I'm not sure I can use it in my code: The problem is that I am maintaining a somewhat (to me) sophisticated site that I did not create. I have looked all over, in vain, to find where $here obtains its value. I'll keep looking. In the meanwhile, however, I lack confidence that I can just assume I'll find the current value in $_GET['here'. One last observation: I can't use your suggested technique for having just a single redirect header because multiple values of $here (in some cases) will be sent to a single location.
-
You don't need isset. You do have to guarantee that $here equals something before you start switching it. And you have to make sure that all of the possible pages you would be redirecting to exist. But also keep in mind that, if the server or network is experiencing a problem, you can get a page unavailable and/or other errors even when all the code is in order. You only need one redirect, but that's a fine point. You could switch to choose the page and then redirect to it:
PHP Code:
<?php
$here = isset($_GET['here'])? $_GET['here'] : '';
switch ( $here ) {
case 'index':
$page = 'http://www.MarainLaw.com/page.php?here=sexual-assault-in-new-jersey';
break;
case 'prostitution-lawyer-nj':
$page = 'http://www.marainlaw.com/page.php?here=prostitution';
break;
default:
$page = 'http://www.MarainLaw.com/page.php?here=sexual-assault-in-new-jersey';
break;
}
header('Location:' . $page); // only one redirect
die();
?>
-
John, that is absolutely phenominal! Thanks again.
A.
-
I take one part of that back. At least for my localhost PHP server, $here does not need to equal anything before it's switched. If it doesn't, then the default is used.
-
Thank you for you help
occur the same problem,Thank you for your help