Acemi PHP kodlama sorun: header fonksiyonu (belki, birisi benim kodunu kontrol etmek gerekir)

4 Cevap php

Aşağıdaki PHP kodu göz önünde bulundurun:

<?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
}
?>

Benim form.html üzerinde Tenis yazarken temelde bu php dosyasına ve süreç veri göndermek ve benim tenis sayfası olan Tn43.html beni yönlendirecektir, kodu yorumlar kendini açıklayıcı olduğunu düşünüyorum. Ne yazık ki, bu işe yaramazsa ve ben gerçekten (Ben bazı büyük aptalca bir hata yaptım biliyorum) ... neden bilmek istiyorum.

PS: Bazı yönlendirmelerde yaparken kullanmak için doğru işlevi header mı?

4 Cevap

Eğer kod biraz yeniden konumlandırmak ve çok değiştirmeniz gerekir:

<?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;
?>

Ben size başlık Yer mutlak adresini gerektiğini düşünüyorum: ... gibi http://yoursite.com/youtypednothing.html

Eğer bu anahtar varsa, kontrol etmek gerekir, o zaman ifadeleri eğer çıkmak zorunda.

<?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;
    }
}
?>

Aklınızda başlığı tutmak () script sonuna kadar tetikleyen almaz veya exit () arayın. Yani şu anda kodu yalnızca söylediğini eminim:

(Pseudocode)

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

Ilk onay if / else all-inclusive saniye ile yazılır.

Sonunda bir çıkış ile bir if / elseif / else blok içine çek koyarak deneyin.