【问题标题】:strange behaviour for $_GET$_GET 的奇怪行为
【发布时间】:2013-05-16 16:11:42
【问题描述】:

我有一个多维语言网站。当调用一个不存在的页面时,.htaccess会在根方向引用404.php

.htaccess 看起来像这样:

ErrorDocument 404 /404.php

根方向上的404.php 只是查看SESSIONCOOKIE 设置的默认语言,并引用语言子方向/language/404.php

404.php 看起来像这样:

include_once "scripts/pref_language.php";

if (isset ($_GET['uri']) === true ){
    $get = $_GET['uri'];

    header("Location: /$pref_language/404.php?uri=$get");
    exit;
}

$url = urlencode($_SERVER['REQUEST_URI']);

header("Location: /$pref_language/404.php?uri=$url");

参考语言子目录,贴出来的URL是这样的:

http://www.example.com/english/404.php?uri=somedata

或使用其他语言:

http://www.example.com/german/404.php?uri=somedata

/english/404.php 的代码是这样的:

<?php
error_reporting(E_ALL);
ob_start();
session_start();
header ("Content-Type: text/html; charset=utf-8");

include_once "../scripts/db_connect.php";
include_once "../scripts/functions.php";
include_once "../scripts/browser_identification.php";
include_once "../scripts/pref_language.php";

...some text variables in its specific language

include_once "../templates/template_404.php";
?>

模板只是一些HTML 样式。将包含在template_404.php 中的页脚是发送数据的主要吸引力。在该页脚中,可以选择更改语言设置和在语言路径之间切换。如果 form 将被提交,SESSIONCOOKIE 将被更改并且页面将启动一个标题功能。

下面是来自页脚的代码:

if (isset($_GET['uri']) ) {
    $uri = urlencode($_GET['uri']);
    echo "TEST URI: $uri";
}

$en_dir = '../en/';
$de_dir = '../de/';
$fr_dir = '../fr/';
$pl_dir = '../pl/';
$tr_dir = '../tr/';

...

$basename = basename($_SERVER['SCRIPT_NAME'], ".php"); 

if ($basename === "index") {
    $form_action = rtrim($_SERVER['PHP_SELF'], 'index.php');
} else{
    $form_action = htmlentities($_SERVER['PHP_SELF']);
}

...

if ( isset($_POST['pref_lang']) === true ) {

    $content = $_POST['pref_lang'];

    if ($content === "de") {

        if ($basename === "about") {
            $basename = "info";
        } else if ($basename === "contact") {
            $basename = "kontakt";
        }           
    }
    $_SESSION['pref_language'] = $_POST['pref_lang'];

    setcookie('pref_language', '', time()- 999999, '/', '.example.de' );
    setcookie('pref_language', $content, time()+60*60*24*365, '/', '.example.de');

    if ($basename === "404"){

        header("Location: ../$content/404.php?uri=$uri");

    } else {

        header("Location: ../$content/$basename");  
    }
}
?>
<div id="data">
    <fieldset>
        <ul>
            <li>
                <form action="<?php echo $form_action;?>" method="post">      
                    <input type="submit" id="de" name="pref_lang" value="de"/><label for="de">german</label>
                </form>
            </li>

            <li>
                <form action="<?php echo $form_action;?>" method="post">  
                    <input type="submit" id="fr" name="pref_lang" value="fr"/><label for="fr">french</label>
                </form>
            </li>

            <li>
                <form action="<?php echo $form_action;?>" method="post">  
                    <input type="submit" id="pl" name="pref_lang" value="pl"/><label for="pl">polish</label>
                </form>
            </li>

            <li>
                <form action="<?php echo $form_action;?>" method="post">  
                                        <input type="submit" id="tr" name="pref_lang" value="tr"/><label for="tr">turkish</label>
                </form>
            </li>

        </ul>
    </fieldset>
</div>

问题是当一个页面有URL的时候

http://www.example.com/**english**/404.php?uri=somedata

我将提交$_POST 将语言更改为德语,例如:

http://www.example.com/**german**/404.php?uri=somedata

例如,$_GET['uri'] 将为空且 URL 如下所示:

http://www.example.com/**german**/404.php?uri=

启动头功能后。而不是标题,我试图回显该行,我将收到一条消息,指出 uri 未定义。

Undefined variable: uri in /customers/7/4/1/example.com/httpd.www/scripts/footer_en.php on line 102
Location: ../english/404.php?uri=

奇怪的是,当我在发送$_POST 之前调用该页面并查看该站点的源代码时,变量$uri 将正确读出$_GET 参数,但稍后它会正确读出在标头函数中不再起作用?

因此,如果有人能告诉我如何处理这个问题,那就太好了。我真的很感激。

非常感谢。

【问题讨论】:

  • 为什么在布尔比较的两个层次上任意停顿?为什么不if (((isset($_GET['uri']) === true) === true) === true)
  • 对不起,我没明白。你能再解释一下吗?
  • 您不需要将isset 的返回值严格与一个布尔值进行比较,该布尔值将产生另一个布尔值,if 然后对其进行评估。 isset 已经返回一个布尔值,只需 if (isset(...)) 就可以了。只是编码风格的挑剔。
  • 好的,谢谢你的提示。

标签: php html variables get global-variables


【解决方案1】:

在表单中使用 uri 之前,将其添加到表单操作中。

$form_action .= "?uri=" . $uri;

【讨论】:

  • 您好,感谢您的回答。添加它时会导致 $_SERVER['PHP_SELF'] 与以前的 URL 相同的错误。 SESSION/COOKIE 上的更改生效,但语言目录不会因为 $_SERVER['PHP_SELF'] 而更改
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-06-08
  • 2015-07-20
相关资源
最近更新 更多