1

当我在尝试回显它的同一页面上清除它时,我遇到了一个奇怪的会话变量为空的问题。这是我正在尝试做的快速类型。

举个例子;

页面 A:

$_SESSION['referer'] = 'abc123';
header('Location: http://www.test.com/pageb.php');
exit();

B页:

function get_referer() {

    $referer = '';
    if (isset($_SESSION['referer'])) {
        $referer = $_SESSION['referer'];
        $_SESSION['referer'] = null;
        unset($_SESSION['referer']);
    }
    echo $referer;
}

现在,当我使用功能在 B 页上执行此操作时...

如果我自己运行它,它可以工作:

   get_referer();

当我在输入的 value 属性中运行 echo 时(确保在页面上只运行一次函数,因为它在被调用后被删除),当我查看页面的源代码时它没有回显任何内容。

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

然而,有趣的是,如果我输入 type="text" 它工作正常。

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

该问题仅出现在 input type="hidden" 的 value 属性上

4

2 回答 2

1

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.

于 2012-05-03T00:45:57.447 回答
0

也许你打电话get_referer()两次?

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.

于 2012-05-03T00:33:43.347 回答