简体   繁体   English

从某页面重定向到另一页面时如何获取引用URL

[英]How to get refering URL when redirected from some page to another page

I am redirecting from several pages to dev.php using php header 我正在使用php标头从几个页面重定向到dev.php

<?php header(Location: dev.php); ?>

I want to know from where i have been redirected here. 我想知道我从哪里被重定向到这里。

I have tried 我努力了

<?php
print "You entered using a link on ".$_SERVER["HTTP_REFERER"];
?>

but it dosent work as $_SERVER["HTTP_REFERER"]; 但它的作用是$_SERVER["HTTP_REFERER"]; only works if you access dev.php using <a href="dev.php">Go to Developer Page</a> 仅当您使用<a href="dev.php">Go to Developer Page</a>访问dev.php时才有效

So how can i get the referring URL? 那么,如何获得推荐网址?

Try something like below. 尝试以下类似的方法。

<?php header(Location: dev.php?referrer=$_SERVER[PHP_SELF]); ?>

OR 要么

header('Referer: '.$_SERVER['PHP_SELF']);
header(Location: dev.php);

If above methods don't work, You will have to go with sessions. 如果上述方法不起作用,则必须进行会话。

You can pass the variable in GET: 您可以在GET中传递变量:

<?php header('Location: dev.php?backurl='.$_SERVER[PHP_SELF]); ?>

And then take it in this way: 然后采用这种方式:

$backurl=$_GET['backurl'];

You need to add the referer to the header manually: 您需要手动将referer添加到标头中:

header("Referer: http://" . $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']); // current page
header("Location: dev.php");

You can also do it using CURL. 您也可以使用CURL。 Have a look at an example function for that here: PHP - Referer redirect script 在这里查看一个示例函数: PHP-引用重定向脚本

$_SERVER["HTTP_REFERER"] = 'YOUR_URL';
header('Location: dev.php');
exit(0);

The referer header is rather unreliable, there are antivirus software and firewalls out there that filter it completely. Referer标头相当不可靠,那里有防病毒软件和防火墙可以对其进行完全过滤。 The only thing you can pretty safely rely on are cookies and with them, sessions. 您唯一可以完全依靠的是cookie,以及与它们相关的会话。

Cookies are stored on the client side, in the browser, therefore can be manipulated, but if you are storing non-critical information, they can be enough. Cookies存储在客户端的浏览器中,因此可以进行操作,但是,如果您存储的是非关键性信息,就足够了。

Sessions nowadays rely on cookies as a means of starting them. 如今的会话依靠cookie作为启动它们的一种方式。 A randomly generated hash is stored in a cookie, the corresponding data is stored on the server linked to this hash. 随机生成的哈希存储在cookie中,相应的数据存储在链接到该哈希的服务器上。 When the client requests the next page, this data is retrieved. 当客户端请求下一页时,将检索此数据。 For more information about sessions read the corresponding section of the PHP manual . 有关会话的更多信息,请阅读PHP手册相应部分

As I wrote, sessions rely on cookies nowadays and you shouldn't do that unless you are aware of the security implications. 如我所写,当今的会话依赖于cookie,除非您意识到安全隐患,否则您不应该这样做。 You can however use URL parameters to pass the session ID along. 但是,您可以使用URL参数传递会话ID。

On a side note: in order to use cookies (and with them sessions) both the setting and the final URL must be on the same domain. 附带说明:为了使用Cookie(以及会话),设置和最终到达网址都必须位于同一域中。 If this isn't the case, you can use request parameters to pass the information along. 如果不是这种情况,则可以使用请求参数来传递信息。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM