-1

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 :)

serendipity
  • 103
  • 1
  • 10
  • you're missing a `?` for your ternary operator. Plus, don't rely on `$_SERVER['HTTP_REFERER']` is not 100% reliable. http://stackoverflow.com/a/6023980/ – Funk Forty Niner Jun 23 '15 at 16:21
  • Ah the "?" is a typo and I'll correct it here ! It is correct in my actual code ^^ I've tried using the variable $HTTP_REFERER without passing by $_SERVER but it didn't work neither :/ – serendipity Jun 23 '15 at 16:27
  • I always take posted code literally ;-) you'd be amazed as to what people actually use, and it's what they post. Call it "experience" ;-) – Funk Forty Niner Jun 23 '15 at 16:27
  • the only way I know to go back 2 pages is with `javascript:window.history.go(-2)` 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. I've seen it done before, but I'd probably have to spend up to 1/2 hour trying to find it. If you want to use a real back button, use sessions and/or cookies. Again, don't rely on `$_SERVER['HTTP_REFERER']`. Consult http://stackoverflow.com/a/6023980/ for the "why". – Funk Forty Niner Jun 23 '15 at 16:32
  • I posted an example for you in an answer below. – Funk Forty Niner Jun 23 '15 at 17:03
  • I'm sorry for this delay guys ! I've been traveling since the last time I posted my question and I couldn't have an access since then. Thank you Fred for your answer which was with a great help to me :) – serendipity Jun 30 '15 at 13:39
  • You're very much welcome Ayoub, glad to have been of help and hope your travels went well, *cheers* – Funk Forty Niner Jun 30 '15 at 13:40

3 Answers3

1

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:

<?php 
session_start();

// you can uncomment it to destroy previously set sessions
// session_destroy();

$_SESSION['back'] = $_SERVER['REQUEST_URI'];

?>

<form action="page2.php" method="post">
<input type="hidden" name="goback" value="<?php echo $_SERVER['REQUEST_URI'] ?>">
...
</form>

Then on the second page: page2.php

<?php 
session_start();

// Your POST information for your contact code goes here

echo "<a href=\"$_SESSION[back]\">Go back</a>";

?>

Or, to have the full http call:

$link = "http://" . $_SERVER['SERVER_NAME'].$_SESSION['back'];

echo "<a href=\"$link\">Go back</a>";

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.

You could also use a header to redirect, but make sure you're not outputting before header.

Ref:


References:


Footnotes:

What I need is the page that the user visited just before getting to that contact.php page.

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.

  • A referer is a link that a person would have clicked from, either from your site or another.

If there was no referer, you can use javascript:window.history.go(-1).

Community
  • 1
  • 1
Funk Forty Niner
  • 74,450
  • 15
  • 68
  • 141
0

If you really want to use $_SERVER['HTTP_REFERER'] (see other comments), then store this in a session when the contact form is loaded so you can use this when the form is submitted.

// Store the referring URL when loading the contact form.
$_SESSION['ref'] = $_SERVER['HTTP_REFERER']

// Redirect user back to referring URL when submitting the form.
header('Location: ' . $_SESSION['ref']);
unset($_SESSION['ref']);
die;

Hope this helps.

Egg
  • 1,782
  • 1
  • 12
  • 28
0
header('Location: '.$_SERVER['HTTP_REFERER']);
die;

Hope it will help you

M.S.Khan
  • 99
  • 1
  • 10