0

Okay so first off, sorry if this is a duplicate. I've searched for around 20 minutes all over stack overflow AND the internet, and I can't seem to find a solution to my problem.

What I'm trying to do:

I have a login form on every page, with a hidden input containing the current page the user is on - so when they login, it successfully redirects back to the page they were on.

E.g. User is on news.php and not logged in. They login, which takes them to login.php to verify data, then redirects back to news.php

This works great!

The problem: If there is any get data or anchor tags at the end of the URL, I can't seem to redirect back to that.

E.g. User is on news.php?id=4#comments and not logged in. They login, etc etc, but it redirects back to news.php and ignores the trailing data.

Anyone have any help here?

My code:

<input type="hidden" value="<?php echo $_SERVER['HTTP_REFERER']; ?>" name="location"  />

$previousPage = $_POST['location'];
header("refresh: 1; url=".$previousPage);

Obviously I think the issue is the $_SERVER['HTTP_REFERER'] part, but I'm not sure what to replace it with to make it include trailing data.

All help is appreciated!

Corey Thompson
  • 398
  • 6
  • 18

2 Answers2

1

Just use

print_r($_SERVER);

and select the one that fits your needs most!

Hint: Combine HTTP_HOST and REQUEST_URI.

The hash portion will actually never be send to the server: Can I read the hash portion of the URL on my server-side application (PHP, Ruby, Python, etc.)?

Community
  • 1
  • 1
Tobias Golbs
  • 4,586
  • 3
  • 28
  • 49
0

Thanks to @Tobias,

This is what I ended up with:

<?php echo basename($_SERVER['REQUEST_URI']); ?><script>document.write(window.location.hash); </script>

Which on a page like example.com/news.php?id=2#comments will return:

news.php?id=2#comments
Corey Thompson
  • 398
  • 6
  • 18