php 重定向后PHP获取上一页网址
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22600559/
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
PHP get previous page url after redirect
提问by Gadgetster
I want to have a navigation bar that tells the user where they just came from. Example: Homepage -> Post
我想要一个导航栏,告诉用户他们刚刚来自哪里。示例:主页 -> 发布
But if they are in their posts manager and click on a post, I want it to say Posts manager -> Post
但是如果他们在他们的帖子管理器中并点击一个帖子,我希望它说帖子管理器 - > 帖子
I read that $_SERVER['HTTP_REFERER']
is not good enough to get the full url so that's not useful as I want the navigation bar all clickable
我读到这$_SERVER['HTTP_REFERER']
不足以获得完整的网址,所以这没有用,因为我希望导航栏全部可点击
Any help is much appreciated!
任何帮助深表感谢!
回答by Wintermute
I believe what you want is called breadcrumbs.
我相信你想要的是面包屑。
What to use for navigation chain storage is actually up to you. You might use even $_SERVER['HTTP_REFERER'] if you want, but that'd be unreliable as it's client-side. Usual way to store such chain is actual URI or session.
导航链存储使用什么实际上取决于您。如果您愿意,您甚至可以使用 $_SERVER['HTTP_REFERER'] ,但这不可靠,因为它是客户端。存储这种链的通常方法是实际的 URI 或会话。
For example, you have such URI: http://www.example.com/post_manager/post
例如,您有这样的 URI:http: //www.example.com/post_manager/post
Then you can iterate through explode("/", $_SERVER["REQUEST_URI"])
to get each step.
然后您可以迭代explode("/", $_SERVER["REQUEST_URI"])
以获取每一步。
That's basic explanation to guide you to a right direction. You can google alot of samples and snippets using keyword breadcrumbs
.
这是引导您走向正确方向的基本解释。您可以使用关键字 google 搜索大量示例和片段breadcrumbs
。
On the topic of saving last visited location (the way to determine wether abonent came from manager or homepage): you can use session's variables to do that. Here's an example:
关于保存上次访问位置的主题(确定是否来自管理器或主页的方式):您可以使用会话的变量来做到这一点。下面是一个例子:
This way you can set a variable on your homepage:
这样你就可以在你的主页上设置一个变量:
<?php
session_start();
$_SESSION['previous_location'] = 'homepage';
?>
And then you just access it from another page:
然后你只需从另一个页面访问它:
<?php
$previous_location = $_SESSION['previous_location'];
?>
It's important to set session.save_pathin your PHP configuration file or your sessions might get lost.
在 PHP 配置文件中设置session.save_path很重要,否则会话可能会丢失。
回答by Triad
You could do it on the client side if you use the Javascript document.referrerproperty. However, a better solution may be to use the global session array.
如果您使用 Javascript document.referrer属性,则可以在客户端执行此操作。但是,更好的解决方案可能是使用全局会话数组。
if (!isset($_SESSION['referrer'])) {
$_SESSION['referrer'] = $current_uri;
} else {
$previous_uri = $_SESSION['referrer'];
$_SESSION['referrer'] = $current_uri;
}
回答by Steve Horvath
The best solution IMO is to save the location into session, every time the user goes to a 'meaningful' page (that you want to be able to navigate back to via this feature), then simply use this array of, say, last 2 visited pages to pull up all the information. Simple and effective.
IMO 的最佳解决方案是将位置保存到会话中,每次用户转到“有意义的”页面(您希望能够通过此功能导航回)时,然后只需使用此数组,例如,最后 2访问过的页面以提取所有信息。简单有效。
回答by DevWL
<?php
session_start();
$_SESSION['user_interactions'][] = $_SERVER['HTTP_REFERER'];
// get previous
$previous_page = end($_SESSION['user_interactions']);
// list all user interactions
foreach($_SESSION['user_interactions'] as $key => $value){
echo $value;
if(count($_SESSION['user_interactions'])-1 != $key) echo ">";
}
?>