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

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

问题描述:

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"

当我在同一页面上清除它时,我遇到一个奇怪的会话变量为空的问题 回应它。 这是我想要做的快速类型。 p>

举个例子; p>

页面A: strong > p>

  $ _ SESSION ['referer'] ='abc123'; 
header('Location:http://www.test.com/pageb.php'); \  nexit(); 
  code>  pre> 
 
 

页面B: strong> p>

  function get_referer(){  
 
 $ referer =''; 
 if(isset($ _ SESSION ['referer'])){
 $ referer = $ _SESSION ['referer']; 
 $ _SESSION ['referer'] = null  ; 
 unset($ _ SESSION ['referer']); 
} 
 echo $ referer; 
} 
  code>  pre> 
 
 

现在当我在页面上执行此操作时 B带有函数... p>

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

  get_referer(); 
   code>  pre> 
 
 

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

 &lt; input type =“hidden”name =“referer”value =“&lt;?php get_referer();  ?&gt;“/&gt; 
  code>  pre> 
 
 

但是,有趣的是,如果我输入type =”text“它可以正常工作。 p> \ n

 &lt; input type =“text”name =“referer”value =“&lt;?php get_referer();  ?&gt;“/&gt; 
  code>  pre> 
 
 

此问题仅发生在输入类型=”隐藏“的值属性 p> div>

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.