However, I am not able to retrieve this data by value in input (the data is always empty).
Generally, when you open the page directly from the browser bar, the HTTP_REFERER
header is not generated.
This is because referrer
refers to a source page. That is, if you came from one page to another.
I suggest that, to put a default value in HTTP_REFERER
if there is nothing, you can put it like this:
<input type="hidden" value="<?php echo isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : $_SERVER['PHP_SELF']; ?>" id="origem" name="origem">
otherwise, the traffic source will always be the form's URL)
In the case of PHP_SELF
, you could put another value, which would be the default value if there was nothing in HTTP_REFERER
. You could register it manually, but I think PHP_SELF
already takes good care of that work.
Have you thought about using a session?
Assuming the user can manipulate this information, I suggest that you use $_SESSION
to store HTTP_REFERER
, if it exists.
For example:
// script_do_formulario.php
session_start();
// Se o header referer existir, ele vai ser adicionado na sessão,
// e não vai sair, ao menos que o usuário entre na página novamente.
if (isset($_SERVER['HTTP_REFERER']) {
$_SESSION['referrer'] = $_SERVER['HTTP_REFERER'];
}
At the time of saving the form data, you will check that the $_SESSION['referrer']
value exists to save it to the bank.
Curiosities
Just for the sake of curiosity, you might be confused about being "referer" or "referrer" (with two "r's"). You have a question about it here: