为什么是我的 header("Location: $_SERVER['HTTP_REFERER']"); PHP 功能不工作?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/8000445/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
why is my header("Location: $_SERVER['HTTP_REFERER']"); PHP function not working?
提问by Simon Suh
It works when I input
当我输入时它工作
header("Location: http://www.google.com");
but it doesn't work when I have
但是当我有时它不起作用
header("Location: $_SERVER['HTTP_REFERER']");
I want to redirect the page to whatever page it came from.
我想将页面重定向到它来自的任何页面。
采纳答案by Rok Kralj
Try it: :)
尝试一下: :)
if (!empty($_SERVER['HTTP_REFERER']))
header("Location: ".$_SERVER['HTTP_REFERER']);
else
echo "No referrer.";
However, for determining which page user came from, I'd rather use session variable, which gets reset at every page:
但是,为了确定用户来自哪个页面,我宁愿使用会话变量,该变量在每个页面都会重置:
session_start();
echo "Previous page:", $_SESSION['loc'];
$_SESSION['loc']=$_SERVER['PHP_SELF'];
ps: This only works for local pages, you cannot track other websites.
ps:这只适用于本地页面,您无法跟踪其他网站。
回答by artlung
You might try:
你可以试试:
header("Location: {$_SERVER['HTTP_REFERER']}");
I've had problems with variable expressions which contain quotes in strings without braces.
我在使用不带大括号的字符串中包含引号的变量表达式时遇到了问题。
You also need to look out for $_SERVER['HTTP_REFERER']
simply not being set. Some user agents don't set it, some privary tools mask it, and you need to handle people coming to your page without a referrer.
您还需要注意$_SERVER['HTTP_REFERER']
根本没有被设置。一些用户代理没有设置它,一些私有工具掩盖了它,你需要处理没有引用者访问你页面的人。
回答by Ahoura Ghotbi
Here is a simple solution. check and see what $_server['http_referer'] is giving you and if its set then you can redirect and if not put a fall back url something like :
这是一个简单的解决方案。检查并查看 $_server['http_referer'] 为您提供的内容,如果已设置,则您可以重定向,如果没有,则放置一个后备网址,例如:
if(isset($_SERVER['HTTP_REFERER']) && $_SERVER['HTTP_REFERER'] != ""){
$url = $_SERVER['HTTP_REFERER'];
}else{
$url = "YOUR INDEX PAGE OR SOMETHING";
}
header("Location: ".$url);
回答by Ben Swinburne
This is a browser feature, and any polite browser will send the correct header (although various 'security' tools will override this with a fake referer).
这是浏览器的一项功能,任何有礼貌的浏览器都会发送正确的标头(尽管各种“安全”工具会用假引用者覆盖它)。
It's browser specific so not every browser/security software combination will send it to the server. You're better off setting a session variable on each page load to determine which page the user came from (or something similar with a bit more logic)
它是特定于浏览器的,因此并非每个浏览器/安全软件组合都会将其发送到服务器。你最好在每个页面加载时设置一个会话变量来确定用户来自哪个页面(或类似的更多逻辑)
回答by Neoaptt
header("Location: $_SERVER[HTTP_REFERER]");
Without the single quotes. This is the fastest way to access and concatenate array values without extra concatenating code.
没有单引号。这是访问和连接数组值的最快方法,无需额外的连接代码。
回答by ddlab
One of the mistakes that occure sometimes is, that NO OUTPUT must happen before header('Location: ' ....)
有时发生的错误之一是,在 header('Location: ' ....)
This is not working (shows the output, but doesn't redirect):
这不起作用(显示输出,但不重定向):
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
echo $res; // <--- OUTPUT COMES HERE
if ($res == true) header("Location: {$referer}");
}
This is working (does redirect properly):
这是有效的(是否正确重定向):
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
//echo $res; // <--- NO OUTPUT COMES HERE
if ($res == true) header("Location: {$referer}");
}
This is also working, but doesn't make sense ():
这也有效,但没有意义():
if (isset($_SERVER['HTTP_REFERER'])) {
$referer = $_SERVER['HTTP_REFERER'];
$cleaned_url = preg_replace('/[^a-z ]+/i', '', strtolower($referer));
$pattern = '/troester/';
$res = preg_match($pattern, $cleaned_url);
if ($res == true) header("Location: {$referer}");
echo $res; // <--- OUTPUT COMES HERE, AFTER header('Location: ' ....)
}
(For better understandig, hope this may help)
(为了更好的理解,希望这可能会有所帮助)
回答by Ramshad
Simply you can use
只需您可以使用
if(isset($_SERVER['HTTP_REFERER'])){
header("Location:".$_SERVER['HTTP_REFERER']."");
}