0

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

.htaccess看起来像这样:

ErrorDocument 404 /404.php

404.php根方向上只是查看由 a 设置的默认语言,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是发送数据的主要吸引力。在该页脚中,可以选择更改语言设置和在语言路径之间切换。如果 aform将被提交,SESSION并且COOKIE将被更改并且页面将启动一个 header-function。

所以这里是来自页脚的代码:

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参数,但后来它在标头函数中不再起作用?

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

非常感谢。

4

1 回答 1

0

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

$form_action .= "?uri=" . $uri;
于 2013-05-16T08:11:42.943 回答