0

我遇到了一个奇怪的问题,我可以在 if-and-else 语句中执行头函数,这些语句应该是无法访问的,例如......

//www.example.com/search/title/harry+potter


$path = explode("/", trim(trim($_SERVER['REQUEST_URI']),"/"));

if(!$path){
    //if array is empty go back to index
    header("Location: /");
}

else if(!$path[1] || !$path[2]){
    //if neither index 1 or 2 are set go back to index
    header("Location: /");
}

鉴于没有满足任何 if-and-else 语句,此代码不应重定向用户,但由于某种原因,它似乎将用户从第二个 else-if 语句重定向。

如果我替换该header()功能echo "test"没有任何反应。

我很困惑,这可能是什么问题?

4

3 回答 3

0

我认为问题在于您如何设置 if 条件。尝试这样的事情:

<?php
//www.example.com/search/title/harry+potter

$URI = "/search/title/harry+potter";

// Placeholder for $_SERVER['REQUEST_URI']
    $path = explode("/", trim( trim( $URI ), "/") );
// Test $path as empty array
    //$path = array();
// Test $path as empty string
    //$path = "";

var_dump($path);

if( !isset($path) || empty($path) ){
    //if array is empty go back to index
    echo "Is empty $ path";    
    // header("Location: /");

} else if ( strlen($path[1]) < 1 && strlen($path[2]) < 1) {
    //if neither index 1 or 2 are set go back to index
    echo "$ path[1] and $ path[2] are both empty";    
    // header("Location: /");

} else {
    echo "$ path is an array with value in position 1 and/or 2";
}

http://codepad.org/fST5A20o

于 2013-11-02T00:14:46.540 回答
0

我可以给你推荐一个头痛解决方案吗?您可以使用URL 重写创建这个简短且对 seo 友好的 url ,让您的生活更轻松。 看看这个

于 2013-11-01T23:58:04.050 回答
0

您可能想检查您的假设。例如,如果$_SERVER['REQUEST_URI']返回字符串/,则explode 函数将返回一个包含两个元素的数组,两个元素都包含一个空字符串。

如果您的 URI//在开头有字符串,那么$path[0]它们$path[1]都是空字符串。

于 2013-11-02T00:09:58.853 回答