0

I'm trying to get the previous URL in my login page using $_SERVER['HTTP_REFERER'], it shows undefined index.

I need to get the referrer URL in domain.com/login, it will redirected from domain.com/subfolder/logout. the logout page redirect to my login page, but I couldn't get the referer URL as the logout URL.

I tried window.history.go(-2); and window.history.go(-1); , it only redirects to empty page not print any value.

  • REFERER is an optional field, the browser / client is not required to send it. – ADyson Mar 11 '20 at 10:27
  • Does this answer your question? [Notice: Undefined index: HTTP\_REFERER](https://stackoverflow.com/questions/19976189/notice-undefined-index-http-referer) – Nico Haase Mar 11 '20 at 10:27
  • With the two previous comments it might be a better idea to save the previous URL in a session variable. That should be easy enough to access. https://www.php.net/manual/en/reserved.variables.session.php – Michael Rennison Mar 11 '20 at 10:29
  • @MichaelRennison not able to get the previous URL to save, that is my issue. is this because of the main domain and subdomain redirection? – Maniamutha Krishnamurthie Mar 11 '20 at 10:39
  • 1
    If the user browsed to the login URL by writing it directly in the address bar, there is no referer... – Honk der Hase Mar 11 '20 at 10:44

3 Answers3

0

it is showing undefined index HTTP_REFERER because when you will be landing first time of page else it will working you can test it like below:

create test.php

<a href="http://localhost/test1.php">asdf</a>
<?php

print $_SERVER['HTTP_REFERER'];
?>

create test1.php

<a href="http://localhost/test.php">asdf</a>
<?php

print $_SERVER['HTTP_REFERER'];
?>

output:

http://localhost/test.php    // => on http://localhost/test1.php
http://localhost/test1.php    // => on http://localhost/test.php
Ronak Dhoot
  • 2,322
  • 1
  • 12
  • 19
0

Try my answer in this post. Use sessions to save your current location and then use to if you want to go back.

Redirect User to the page they've left off once they had log in

gilbertdim
  • 357
  • 2
  • 11
-1

you can use $_SESSION[] for your pages .

almoo900
  • 7
  • 5