tags:

views:

84

answers:

4

Hi, consider the following PHP code:

<?php
$searchsport = $_REQUEST['sport'];
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);
header("Location: ".$sportarray[$searchsport].".html"); //directs user to the corresponding page they searched
if ($searchsport == NULL) {
header("Location: youtypednothing.html"); //directs user to a page I've set up to warn them if they've entered nothing
} else {
header("Location: sportdoesnotexist.html"); //if sport isn't in my root, a warning will appear
}
?>

I think the code comments are self-explanatory, basically when I type Tennis on my form.html it will send data to this php file and process and direct me to Tn43.html which is my tennis page. Unfortunately, it doesn't work and I really want to know why... (I know I've made some huge silly mistake).

PS: Is header the right function to use when doing some redirecting?

A: 

I think you need absolute address in your header Location: ... like http://yoursite.com/youtypednothing.html

Kamil Szot
I'll try it, but will it change anything? all my stuff is on the same folder of the site.
Haskella
I believe relative addresses are permitted as well: http://ditio.net/2008/10/04/php-redirect-header-location/
nc3b
Yeah, relative isn't the problem, the final logic is.
Tchalvak
+3  A: 

You should re-position your code a bit and modify too:

<?php
$searchsport = $_REQUEST['sport'];

$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);

if (!$searchsport) {
header("Location: youtypednothing.html"); //directs user to a page I've set up to warn them if they've entered nothing
exit;
} elseif (!in_array($searchsport, $sportarray)) {
header("Location: sportdoesnotexist.html"); //if sport isn't in my root, a warning will appear
exit;
}

header("Location: ".$sportarray[$searchsport].".html"); //directs user to the corresponding page they searched
exit;
?>
Sarfraz
Okay... you're definitely more helpful than my boyfriend =)It works. Can you explain why?
Haskella
@Haskella: Because your `if-else` condition was wrong, either was executing and never reaching to your last `header` line. `else` was not needed there causing the following `header` to run and nothing below that allowed to execute.
Sarfraz
I can be your boy friend, hahahha, just kidding :)
Sarfraz
A: 

Keep in mind that header() doesn't get triggered until the end of the script or you call exit(). So I'm pretty sure that right now your code is only saying:

(pseudocode)

If null, send to the null page, else send to the default page.

The first check will be overwritten by the all-inclusive second if/else.

Try putting the check into a if/elseif/else block with an exit at the end.

Tchalvak
A: 

you need to check if that key exists, then you have to exit the if statements.

<?php
$searchsport = $_REQUEST['sport'];
$sportarray = array(
"Football" => "Fb01",
"Cricket" => "ck32",
"Tennis" => "Tn43",
);

if(isset($sportarray[$searchsport])){
    header("Location: ".$sportarray[$searchsport].".html"); //directs user to the corresponding page they searched
    die;
} else {
    if ($searchsport == NULL) {
        header("Location: youtypednothing.html"); //directs user to a page I've set up to warn them if they've entered nothing
        die;
    } else {
        header("Location: sportdoesnotexist.html"); //if sport isn't in my root, a warning will appear
        die;
    }
}
?>
Mihai Iorga
so an echo can direct? what does die do?
Haskella
The die actually exits the -script-, not the if statement. Exiting the script via die or exit is what will trigger the `header("Location: something.html")` statement to actually act.
Tchalvak
Why the downvote?
Salman A