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
Any help is much appreciated!
Use the HTTP_REFERER Request Header to Return to the Previous Page in PHP. The HTTP_REFERER request header returns the URL of the page from where the current page was requested in PHP. The header enables the server to acknowledge the location from where the users are visiting the current page.
You can use the document. referrer property to get the previous page URL in JavaScript. It is a read-only property that returns the URL of the document that loaded the current document.
There are two approaches used to redirect the browser window back. Approach 1: Using history. back() Method: The back() method of the window. history object is used to go back to the previous page in the current session history.
Get Last URL Segment If you want to get last URI segment, use array_pop() function in PHP.
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.
For example, you have such URI: http://www.example.com/post_manager/post
Then you can iterate through explode("/", $_SERVER["REQUEST_URI"])
to get each step.
That's basic explanation to guide you to a right direction. You can google alot of samples and snippets using keyword 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_path in your PHP configuration file or your sessions might get lost.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With