I am having a strange issue of a session variable being empty when I clear it on the same page that I attempt to echo it. Here is a quick type-up of what I am trying to do.
Take this example;
Page A:
$_SESSION['referer'] = 'abc123';
header('Location: http://www.test.com/pageb.php');
exit();
Page B:
function get_referer() {
$referer = '';
if (isset($_SESSION['referer'])) {
$referer = $_SESSION['referer'];
$_SESSION['referer'] = null;
unset($_SESSION['referer']);
}
echo $referer;
}
Now when I do this on page B with the functions…
If I run it all by itself it works:
get_referer();
When I run the echo inside the value attribute of the input (making sure to only run the function once on the page due to it being erased after being called), it didn’t echo anything when I view source the page.
<input type="hidden" name="referer" value="<?php get_referer(); ?>" />
However, funny enough, if I make the input type=”text” it works fine.
<input type="text" name="referer" value="<?php get_referer(); ?>" />
The issue only occurs on the value attribute for input type=”hidden”
You’re outputting the content with
you’re not viewing it on the same page as you would have if you were using
type="text". When usingtype="hidden", you’re most likely right-clicking the window and choosing View Source in your browser. The problem is that same browsers (like Chrome) refresh the page when you do so. This means, that once you load the page, thevalueattribute actually containsabc123, but when you attempt to see it, the page is refreshed, and therefore the session no longer exists, hencevalueis empty.