如果在同一页面上清除会话变量将不会回显?

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

<input type="hidden" name="referer" value="<?php get_referer(); ?>" />

you're not viewing it on the same page as you would have if you were using type="text". When using type="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, the value attribute actually contains abc123, but when you attempt to see it, the page is refreshed, and therefore the session no longer exists, hence value is empty.

Maybe you are calling the get_referer() twice?

In the first it will echo the referer and unset. When you call in the input, the referer don't exists anymore in the session, so prints nothing.