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

时间:2013-05-17 08:58:18

标签: php html variables get global-variables

我已经对此问题提出了疑问,但又有一个问题属于$_GET和标题函数:

我有一个多语种网站。调用不存在的页面时,.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将被更改,页面将启动标题功能。

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

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>

4 个答案:

答案 0 :(得分:1)

如果我理解正确,你想要来自:

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">

答案 1 :(得分:1)

扩展jwheel12的答案。表单操作将作为不包含http://的相对路径。因此,将您的action =“”附加到当前网址。如果可能,请使用绝对路径。

<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;
};

最后,如果您只想在2种语言之间切换,可以将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>';

答案 2 :(得分:0)

来自文档:

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://。

也许尝试类似的东西?

答案 3 :(得分:0)

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

只是一个想法。我可以想象它对大型网站来说可能不太实用。