0

I was wondering if this is possible: I have a button on "page #1" and when clicking on it, it redirects me to /test.php file.

I would like to echo the full url of the website I was redirected from ("Page #1") to /test.php file.

I have tried $_SERVER['HTTP_REFERER'] and some other ways to that I have found from Google, but still no luck. Which raises the question, whether this is possible or not, since PHP is server-sided?

Thanks in advance

Kaspar
  • 89
  • 1
  • 9
  • 3
    Possible duplicate of [Get original URL referer with PHP?](https://stackoverflow.com/questions/1864583/get-original-url-referer-with-php) – William Perron Jan 23 '18 at 13:34
  • $_SERVER['HTTP_REFERER'] should indeed work. Maybe try doing a print_r($_SERVER) in test.php and take a look through the environment variables you have access too. What are you seeing in $_SERVER['HTTP_REFERER'] if not page#1's url? – filip Jan 23 '18 at 13:42
  • HTTP_REFERER will get "lost" when you refresh a page, or atleast navigate to it by hand. – Splinti Jan 23 '18 at 13:44
  • Also HOW are you redirecting? Is this a link? Or a javascript redirect when the button is clicked? Something else? – filip Jan 23 '18 at 13:54
  • I redirect it with a link to my .php file. "Notice: Undefined index: HTTP_REFERER in ".. anyhow I put it. This might be a stupid question, I haven't done PHP so much – Kaspar Jan 23 '18 at 13:54
  • Yea, sounds like HTTP_REFERER is not being passed over in your case. Which ultimately be common as it is up to the client to send this. You may be able to tweak your configuration to get this to work but the answer @Splinti gave below may be more reliable. – filip Jan 23 '18 at 14:10

2 Answers2

2

I have an idea:

In your config.php or some file, that executes on every page that a user visits, you could add a session variable:

$_SESSION['LAST_PAGE'] = $_SERVER['REQUEST_URI']

then access it when you need it. Or even use it as a 'history' of pages:

array_push($_SESSION['LAST_PAGE'], $_SERVER['REQUEST_URI'])

Splinti
  • 385
  • 3
  • 13
0

You can use cookies to do this. For example in Page#1.php you can set this:

<?php
$cookie_name = "url";
$cookie_value = "http://your.link.com/page#1.php";
setcookie($cookie_name, $cookie_value, time() + (3600 * 1), "/"); // Expires in 1 hour
?>

Then on your Test page (test.php) you can echo or console.log() this cookie like this:

  <?php
    if(!isset($_COOKIE[$cookie_name])) {
        echo "Cookie name '" . $cookie_name . "' is not set!";//You check if the cookie is saved
    } 
    else {
        echo "You redirected from '" . $cookie_name . "' : " . $_COOKIE[$cookie_name];
    }
    ?>

And of course you can console it like this:

<?php
  console.log($_COOKIE[$cookie_name]);
?>

Finally, there is an other way that it has to deal with JS and window.location.href or document.URL commands. You can check this answer about it.

Hope this helps, although there might be better solutions about it!

Alcaeus D
  • 258
  • 3
  • 18
  • Thank You for your fast response. I am unable to edit Page#1.php but I can edit that specific button html only. – Kaspar Jan 23 '18 at 13:46
  • Please provide us some code about this because if the only thing you can do is working on a HTML button then it's difficult to find a solution. For things like this it's common to implement scripts or PHP code to get it work. – Alcaeus D Jan 23 '18 at 13:52
  • It is a fairly closed-source webpage. I can make a button in html and use links to redirect it to another websites. "

    ". So I directed the button to open my .php file, which I placed in the server. In this file, I would like to echo the page's URL I was directed from (page where I created the button). If it's not possible under my conditions then I just have to find a workaround :)
    – Kaspar Jan 23 '18 at 14:12
  • First of all, the code you just copied is a paragraph with a hyperlink inside it that you are using as a "button" with your css. It is not a button. Secondly, how are you creating buttons in a PHP/HTML file without having full access on it ? If this is a i.e. Wordpress site then you should locate the file you are writing the HTML, on the server. Then do the necessary modifications. – Alcaeus D Jan 23 '18 at 14:19