Php 为什么$\u GET不能在header函数中工作?
我对此问题已有疑问,但还有一个问题属于Php 为什么$\u GET不能在header函数中工作?,php,html,variables,get,global-variables,Php,Html,Variables,Get,Global Variables,我对此问题已有疑问,但还有一个问题属于$\u GET和标题函数: 我有一个多语言网站。调用不存在的页面时,.htaccess将引用根目录中的404.php .htaccess如下所示: ErrorDocument 404 /404.php <?php error_reporting(E_ALL); ob_start(); session_start(); header ("Content-Type: text/html; charset=utf-8"); include_once ".
$\u GET
和标题函数:
我有一个多语言网站。调用不存在的页面时,.htaccess
将引用根目录中的404.php
.htaccess
如下所示:
ErrorDocument 404 /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";
?>
根方向上的404.php
只查看由会话或COOKIE设置的默认语言,并引用语言子目录/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");
参考language子目录,发布的URL
如下所示,其中参数uri
仍然不同:
http://www.example.com/english/404.php?uri=somedata
或使用另一种语言:
http://www.example.com/german/404.php?uri=somedata
/english/404.php
的代码如下所示:
ErrorDocument 404 /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";
?>
问题是当一个页面的URL
为
http://www.example.com/**english**/404.php?uri=somedata
我将提交$\u POST
将语言更改为德语,如:
http://www.example.com/**german**/404.php?uri=somedata
例如,$\u 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=
奇怪的是,当我在发送$\u POST
之前调用页面并查看站点的源代码时,变量$uri
将正确读取$\u GET
参数,但稍后它在header函数中不再起作用。问题是,为什么这样做不起作用
因此,如果有人能告诉我如何处理这一问题,那就太好了。我真的很感激
非常感谢
更新:
我试图将$\u GET
参数保存到会话
中,但是当发布表单并重定向到其他语言站点时,会话
的内容似乎是错误的,因为它不是$\u GET
参数,而是来自html的css链接头状css/colorbox.css
此时,我试图通过将表单操作设置为该值来获取该值:
...
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>
。。。
如果($basename==“索引”){
$form_action=rtrim($_SERVER['PHP_SELF','index.PHP');
}
如果($basename==“404”){
$form_action=“”;
}
如果($basename!=“index”和$basename!=“404”){
$form_action=htmlentities($_SERVER['PHP_SELF']);
}
...
如果我理解正确,您希望从:
http://www.example.com/**english**/404.php?uri=somedata
致:
使用表单提交按钮。要做到这一点,您需要在标记上设置操作,以包括来自当前页面的uri,从而将其传递到新的语言404页面
<form action="<?php echo $form_action . ((isset($_GET['uri']))?$_GET['uri']:""); ?>" method="post">
文档中的:
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;
?>
注意:
HTTP/1.1需要一个绝对URI作为»位置:包括方案、主机名和绝对路径的参数,但有些客户端接受相对URI。通常,您可以使用$\u服务器['HTTP\u HOST']、$\u服务器['PHP\u SELF']和dirname()从相对URI生成绝对URI:
来扩展jwheel12的答案。表单操作将作为相对路径使用,其中不包含http://。因此,将您的操作“”附加到当前URL。如果可能,请使用绝对路径
<form action="http://www.example.com/english/404.php<?php echo ( $_SERVER['QUERY_STRING'] )?
"?".$_SERVER['QUERY_STRING']) : "" );?>" method="post">
最后,如果您只想在两种语言之间切换,可以使用preg_替换为超链接
$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>';
$currentURL=“http://$\u服务器[http\u主机]$\u服务器[REQUEST\u URI]”;
if(preg_匹配('/derman/i',$currentURL)){
$newURL=preg_replace('/derman/i','english',$currentURL);
$currentLanguage=“德语”;
}
否则{
$newURL=preg_replace('/english/i','derman',$currentURL);
$currentLanguage=“英语”;
};
回声';
停留在同一页面上,但内容发生变化不是更容易吗?
如果语言为英语,则加载英语404信息,对于德语,加载德语,等等
只是一个想法。我可以想象,对于较大的站点来说,这可能不实用。GET将消失,因为提交后页面将刷新,并且没有给出GET参数。您可以将其存储在会话变量中?老实说:TL;但对我来说很明显的是,在将其作为查询参数再次附加之前,需要对值进行正确的URL编码。$\u GET
中的值已自动进行URL解码。请看@Marijke,我将在会话中完成它,它的内容是一个完整的,而不是它应该是。由于某些原因,会话将类似于css/colorbox.css。。。这是我不理解的。为什么要重定向到特定语言的404模板?不适合你吗?你好,谢谢你回答这个问题。我试着用你的方法,但没有我想象的效果。我更新了我的问题。谢谢。也许可以发布上面代码创建的url,以便我可以调整它?好的,您的代码创建了该url:http://www.example.com/english/404.php/english/404
如果设置了$\u GET
参数,您的代码似乎只是替换了该参数。如果将代码更改为:您好,谢谢您回答此问题。我使用了你的提示来获取这个,但仍然有一个小问题。当我发布表单时,将显示正确的语言,但页脚本身将是错误的。会话/cookie上的更改将生效,因此我猜站点将被重新加载,但页脚不会被加载。谢谢
$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;
};
$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>';