1

I have page 'foo', which I have no access to and cannot change the code of, and a page 'bar', a page on my website that I can change the code of.

I don't know what the URL of 'foo' is.

If page 'foo' redirects to another page 'bar', how could I use PHP on 'bar' to find the URL of 'foo'?

I've tried $_SERVER['HTTP_REFERER'], but this doesn't pick up on page redirects and returns nothing after a page redirect from foo to bar.

It isn't possible for me to put any code on 'foo'.

Giulio Caccin
  • 2,962
  • 6
  • 36
  • 57
  • possible duplicate of [Back to previous page with header( "Location: " ); in PHP](http://stackoverflow.com/questions/5285031/back-to-previous-page-with-header-location-in-php) – D4V1D Mar 30 '15 at 14:55
  • `$_SERVER['referer']` should not be trusted anyway. There are some programs on the web that claim to do backlink checks and do give some surprising results but I'm not sure these would work for you but worth a shot, otherwise if you don't know where foo is, your only reference points (such as `SERVER['referer']`) can not be trusted as they can be manipulated by the sender. – Martin Mar 30 '15 at 15:00

5 Answers5

1

You can store that information into a session :

session_start();
$_SESSION['frompage'] = 'foo';
header('location:bar'); 

Then, in the foo page :

session_start();
echo $_SESSION['frompage']
blue112
  • 52,634
  • 3
  • 45
  • 54
  • Sorry, I should have made it clearer. I don't know the URL of foo, but want to find it out. –  Mar 30 '15 at 14:54
1

Depending on the situation, one method could be to send a reference to the original page via the URL

So redirect foo like this;

www.example.com/bar.php?ref=foo

And then on bar.php

$original_page = $_GET['ref']
paddyfields
  • 1,466
  • 2
  • 15
  • 25
  • As I said, I don't have access to the code on page 'foo'. Thanks for your help anyway. –  Mar 30 '15 at 14:56
1
  • For those going through this page and still haven't found the answer.

Use the following code in bar.php

<?php
// check if your page actually came from an external link
if (empty($_SERVER['HTTP_REFERER'])){ 
    $log = ""; //if no, set $log to nothing (or you can put a value)
}else{
    $log= $_SERVER['HTTP_REFERER']; 
    //if yes, then set log to the url of the page where the link to your page was clicked
}

?>

You can now echo $log if you want, it would give you the url of the referrer (if truly a link was clicked). But if user types it directly on the browser address bar, $log is set to an empty string.

Hope this helps

  • `$log = $_SERVER['HTTP_REFERER'] ?? '';` would be cleaner - but I don't see how this answers the question given it is implied that this is what is already happening on page `bar` – AD7six Feb 28 '21 at 13:59
  • The end result is to retrieve foo's URL, In the question: **If page 'foo' redirects to another page 'bar', how could I use PHP on 'bar' to find the URL of 'foo'?** , and `$log = $_SERVER['HTTP_REFERER'] ?? '';` is clearer. – Prince Special Feb 28 '21 at 19:42
0

Save your content in a session variable. If its the same domain you can easily use sessions.

session_start();
$_SESSION['myvar'] = 'test';
René Höhle
  • 26,716
  • 22
  • 73
  • 82
0

You can set it via get.

bar.php

<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
header("Location: foo.php?referer=".$url);
?>

Then in foo:

echo $_GET['referrer'];
Phate01
  • 2,499
  • 2
  • 30
  • 55