为什么$_GET不能在header-function中工作


why does $_GET not work in header-function?

我已经对这个问题有了疑问,但进一步的问题属于$_GET和header-function:

我有一个多语种网站。当调用一个不存在的页面时,.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']) ){
    $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看起来像这样,其中参数uri仍然不同:

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中包含的页脚是发送数据的主要吸引力。在该页脚中有更改语言设置和在语言路径之间切换的选项。如果一个表单将被提交,SESSIONCOOKIE将被更改,页面启动一个header函数。

这里是来自页脚的代码:

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>

问题是当一个页面有

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参数,但后来它在头函数中不再工作。问题是,为什么这不起作用?

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

谢谢。

更新:

我试图将$_GET参数保存为SESSION,但当发布表单并重定向到其他语言网站时,SESSION的内容似乎是错误的,因为它不会是$_GET参数,而不是它将是来自html头部的css链接,如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>

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

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

:

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

使用表单提交按钮。要做到这一点,您需要设置标记上的操作,使其包含来自当前页面的uri,以便将其传递到新语言404页面。

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

展开jwheel12的答案。表单动作将作为不含http://的相对路径工作。因此,将action="附加到当前URL。如果可能,使用绝对路径。

<form action="http://www.example.com/english/404.php<?php echo ( $_SERVER['QUERY_STRING'] )?
"?".$_SERVER['QUERY_STRING']) : "" );?>" method="post">

或者您也可以将变量作为隐藏的输入类型

传递。
foreach ($_GET as $key => $value) {
echo '<input type="hidden" name="'.$key.'" value"'.$value.'">';
}

我喜欢使用像下面这样的自定义函数,它将循环遍历数组"$ary"中的所有项,并返回最后一个不为空的项。

$myVariable = getVar('myVariable');
function getVar($var) {
$ary = array('_SESSION', '_COOKIE', '_POST', '_GET');
foreach ($ary as $a) {
    if ($GLOBALS[$a][$var] != '') {
        $return = $GLOBALS[$a][$var];
    };
};
return $return;
};

最后,如果你只想在两种语言之间切换,你可以使用preg_replace和超级链接

$currentURL = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
if (preg_match('/german/i', $currentURL)){
$newURL = preg_replace('/german/i', 'english',$currentURL);
$currentLanguage = "german";
}
else {
$newURL = preg_replace('/english/i', 'german',$currentURL);
$currentLanguage = "english";
};
echo '<a href ="'.$newURL. '">'($currentLanguage == "german"? "View in English": "Sehen Sie auf Deutsch";). '</a>';

来自文档:

Note:
HTTP/1.1 requires an absolute URI as argument to » Location: including the scheme, hostname and absolute path, but some clients accept relative URIs. You can usually use $_SERVER['HTTP_HOST'], $_SERVER['PHP_SELF'] and dirname() to make an absolute URI from a relative one yourself:
<?php
/* Redirect to a different page in the current directory that was requested */
$host  = $_SERVER['HTTP_HOST'];
$uri   = rtrim(dirname($_SERVER['PHP_SELF']), '/''');
$extra = 'mypage.php';
header("Location: http://$host$uri/$extra");
exit;
?>

另外,我认为FireFox和其他抱怨HTTP 1.1的浏览器都需要http://。

也许可以试试这样的东西?

保持在同一页面上,但内容发生变化不是更容易吗?如果语言是英语,加载英语404信息,对于德语加载德语,等等。

只是一个想法。