$ _GET的奇怪行为

时间:2013-05-16 07:59:56

标签: php html variables get global-variables

我有一个多维语言网站。在调用不存在的网页时,.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']将为空,网址如下:

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参数正确但后来它在标题函数中不再起作用了吗?

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

非常感谢。

1 个答案:

答案 0 :(得分:0)

将uri添加到表单操作中,然后在表单中使用它。

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