I'm using php and I'm trying to add a functionality to my contact form that sends me the page that precedes the contact page. I added this line of code on my form code:
$httpReferer = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : null;
$e_body = "Provenance : $httpReferer ";
This works fine and shows me a link, but the problem is that the link is the same contact page (http://...contact.php). What I need is the page that the user visited just before getting to that contact.php page.
Anyone knows why is this happening? Is there a way to go back 2 pages instead of one?
Thanks :)
One way to have a back button, is to add a hidden input to your form (seeing that is what you are using in a contact page), and using
$_SERVER['REQUEST_URI']
as a value and assigning a session array to it and using sessions. Cookies could also be used, but I've used sessions in my example.First start the session:
Then on the second page:
page2.php
Or, to have the full
http
call:Sidenote: As I stated in comments,
The only way I know to go back 2 pages is with
javascript:window.history.go(-2)
which you could implement that in PHP with an echo.Another way would be to use a breadcrumb method, but that would require some fancy work.
$_SERVER['HTTP_REFERER']
isn't fully reliable.You could also use a header to redirect, but make sure you're not outputting before header.
Ref:
References:
Footnotes:
If there wasn't a referer to the page, then there is no way for a user to go back, because there is nothing to go back to, unless they use their browser's back arrow button.
If there was no referer, you can use
javascript:window.history.go(-1)
.