Php 如何将目录从if语句排除到标题页
你好,我通过一个不起作用的头函数发疯了 这个想法是,当用户调用foo.php页面时,他会被误导。therfor我包括了一个来自script.php的脚本 该脚本从中读取有关接受的语言的信息 $\u服务器[“HTTP\u接受\u语言”]) 好的,从这里我有了首选语言和一个国家代码,我需要将foo.php引入正确的语言方向。script.php中的代码如下所示:Php 如何将目录从if语句排除到标题页,php,html,if-statement,header,Php,Html,If Statement,Header,你好,我通过一个不起作用的头函数发疯了 这个想法是,当用户调用foo.php页面时,他会被误导。therfor我包括了一个来自script.php的脚本 该脚本从中读取有关接受的语言的信息 $\u服务器[“HTTP\u接受\u语言”]) 好的,从这里我有了首选语言和一个国家代码,我需要将foo.php引入正确的语言方向。script.php中的代码如下所示: if ($pref_language == 'af'){ header('Location:en'.$_SERVER['SCRIP
if ($pref_language == 'af'){
header('Location:en'.$_SERVER['SCRIPT_NAME']);
exit;
}
if ($pref_language == 'sq'){
header('Location:en'.$_SERVER['SCRIPT_NAME']);
exit;
}
因此,这些示例将把foo.php头文件发送到
root/en/foo.php
现在,当只调用位于root/scripts/script.php中的script.php时,我将以root/en/scripts/script.php为标题。这就是为什么我想添加if条件
标题为()的所有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;
}
}
这是行不通的。所以如果有人能帮我,我真的很感激
非常感谢。这可能是您的解决方案,虽然在某种程度上有点傻,但可以完成以下任务:
<?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;
}
}
?>
另一种方法是正则表达式,但这更简单
我建议你在选择语言时更具活力。i、 e:
<?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;
}
?>
如果(isset($path==false))
?如果此处设置为false,则您正在签入。如果($path)
,它不应该是吗?除了足球,我不知道“头球”是什么意思。你能澄清你的问题吗?这意味着url不应该是$path。因此它将从头中排除根/scripts/file.ext目录。。。。这毫无意义。是否要查看变量是否已设置?这是确定设置的,仅在isset($path==false)上方2行。
语法无效:致命错误:无法对表达式的结果使用isset()。在您的例子中只需使用if($path)
。我还建议您更改if()语句。我在答案中加上这个。。。