为什么$ _GET在header-function中不起作用?

bon*_*nny 6 html php variables get global-variables

我已经对这个问题提出了一个问题,但是另一个问题是归属$_GET和标题函数:

我有一个多语种网站.调用不存在的页面时,.htaccess404.php在根目录中引用.

.htaccess如下所示:

ErrorDocument 404 /404.php
Run Code Online (Sandbox Code Playgroud)

404.php在根方向简单地着眼于其中的默认语言是由一组SESSIONCOOKIE并指的是语言子目录/language/404.php

404.php看起来像这样:

include_once "scripts/pref_language.php";

if (isset ($_GET['uri']) ){
    $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");
Run Code Online (Sandbox Code Playgroud)

提到语言子目录,发布URL看起来像参数uri仍然不同:

http://www.example.com/english/404.php?uri=somedata
Run Code Online (Sandbox Code Playgroud)

或用另一种语言:

http://www.example.com/german/404.php?uri=somedata
Run Code Online (Sandbox Code Playgroud)

/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";
?>
Run Code Online (Sandbox Code Playgroud)

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

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

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

...

$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']) ) {

    $content = $_POST['pref_lang'];

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

        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="german" name="pref_lang" value="german"/><label for="german">german</label>
                </form>
            </li>
        </ul>
    </fieldset>
</div>
Run Code Online (Sandbox Code Playgroud)

问题是当一个页面有URL.时

http://www.example.com/**english**/404.php?uri=somedata
Run Code Online (Sandbox Code Playgroud)

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

http://www.example.com/**german**/404.php?uri=somedata
Run Code Online (Sandbox Code Playgroud)

例如$_GET['uri']将是空的,URL看起来像:

http://www.example.com/**german**/404.php?uri=
Run Code Online (Sandbox Code Playgroud)

启动头功能后.我试图回应那条线而不是标题,我将收到一条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=
Run Code Online (Sandbox Code Playgroud)

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

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

非常感谢.

更新:

我试图将$_GET参数保存到一个SESSION但是当发布表单并被重定向到另一个语言网站时,内容SESSION似乎是错误的,因为它不是$_GET参数而不是它将来自css链接来自html头像css/colorbox.css

目前我尝试通过设置form action为:

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

if ($basename === "404") {
    $form_action = "";
}

if  ($basename !== "index" AND $basename !== "404") {
    $form_action = htmlentities($_SERVER['PHP_SELF']);
}
...

<li>
    <form action="<?php echo $form_action. ( (isset($_GET['uri']) ) ? "/english/?uri=".urlencode($_GET['uri']) : "" );?>" method="post">
        <input type="submit" id="english" name="pref_lang" value="en"/><label for="en">englisch</label>
    </form>
</li>
Run Code Online (Sandbox Code Playgroud)

小智 1

如果我理解正确的话,你想从:

http://www.example.com/**english**/404.php?uri=somedata
Run Code Online (Sandbox Code Playgroud)

到:

http://www.example.com/**german**/404.php?uri=somedata
Run Code Online (Sandbox Code Playgroud)

使用表单提交按钮。为此,您需要设置标签上的操作以包含当前页面的 uri,以便将其传递到新语言 404 页面。

<form action="<?php echo $form_action . ((isset($_GET['uri']))?$_GET['uri']:""); ?>" method="post">
Run Code Online (Sandbox Code Playgroud)