Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP get previous page url after redirect

Tags:

php

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!

like image 937
Gadgetster Avatar asked Mar 24 '14 03:03

Gadgetster


People also ask

How can I get previous page in PHP?

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.

How do I find the URL of a previous 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.

How do I redirect a previous URL?

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.

How can I get the last part of a URL in PHP?

Get Last URL Segment If you want to get last URI segment, use array_pop() function in PHP.


1 Answers

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.

like image 112
Wintermute Avatar answered Oct 07 '22 22:10

Wintermute