如何将目录从if语句排除到标题页

时间:2012-12-13 19:34:07

标签: php html if-statement header

你好,我通过一个不起作用的标题函数而疯狂。

这个想法是,当用户正在调用页面foo.php时,他将被加盖。因为我包含了来自script.php的脚本

该脚本从

中读出有关所接受语言的信息
  

$ _ SERVER [ “HTTP_ACCEPT_LANGUAGE”])

好吧,从那里我有首选语言和国家代码,我需要将foo.php标题为正确的语言方向。 script.php中的代码如下:

if ($pref_language == 'af'){
    header('Location:en'.$_SERVER['SCRIPT_NAME']);
    exit;
}
if ($pref_language == 'sq'){
    header('Location:en'.$_SERVER['SCRIPT_NAME']);
    exit;
}

所以这些例子会将foo.php标题为

目录
  

根/ EN / foo.php

现在当调用位于root / scripts / script.php中的script.php时,我将对root / en / scripts / script.php进行调用。这就是为什么我想添加if条件 所有带有header()的if语句;只发生在root / scripts /.

之外的文件

所以我补充说:

$filename = $_SERVER['SCRIPT_NAME'];
$path = $_SERVER['HTTP_HOST']."/scripts".$_SERVER['SCRIPT_NAME'];

if (isset($path == false)){

    if ($pref_language == 'af'){
        header('Location:en'.$_SERVER['SCRIPT_NAME']);
        exit;
    }
    if ($pref_language == 'sq'){
        header('Location:en'.$_SERVER['SCRIPT_NAME']);
        exit;
    }
}

不起作用。所以,如果有人能帮助我,我真的很感激。

非常感谢。

1 个答案:

答案 0 :(得分:1)

这可能是你的解决方案,虽然在某种程度上它有点愚蠢,但会完成这项工作:

<?php

$unwanted_dir = "/scripts";

// this will make sure that the script name doesnt start with "/scripts" 
if (substr($_SERVER['SCRIPT_NAME'], 0, strlen($unwanted_dir)) != $unwanted_dir){

    if ($pref_language == 'af'){
        header('Location:en'.$_SERVER['SCRIPT_NAME']);
        exit;
        }
    if ($pref_language == 'sq'){
        header('Location:en'.$_SERVER['SCRIPT_NAME']);
        exit;
        }
}
?>

另一种方式是正则表达式,但这是更简单的

我建议你在选择语言时更有活力。即:

<?php

$unwanted_dir = "/scripts";
$pref_language == 'af';  // dynamicaly set the language
$full_path = '/home/php/site/';

// this will make sure that the script name doesnt start with "/scripts" 
if (substr($_SERVER['SCRIPT_NAME'], 0, strlen($unwanted_dir)) != $unwanted_dir){

    if (is_dir($full_path . $pref_language)){
        header('Location:'$full_path . $pref_language . $_SERVER['SCRIPT_NAME']);
    }
    else{
        echo "Sorry, we don't support your language";
        // or
        // header('Location:go/to/unsopported/languages.php');
    }

    exit;
}
?>