简体   繁体   English

Webmatrix session_destroy()

[英]Webmatrix session_destroy()

I have written a simple code for $_SESSION variable in the first php file: 我在第一个php文件中为$ _SESSION变量编写了一个简单的代码:

 <?php
    session_start();
    $_SESSION["name"] = "John";
 ?>

and in another php file to render this: 并在另一个php文件中呈现此内容:

  <?php
      session_start();          
      echo $_SESSION["name"];
   ?>

But after that i used session_unset() ; 但是之后我使用了session_unset() ; and session_destroy() ; session_destroy() ; and after that i can't render any new $_SESSION variable nor the existing one. 之后,我将无法渲染任何新的$_SESSION变量或现有的变量。 I am using Microsoft WebMatrix program and Chrome as main browser. 我正在使用Microsoft WebMatrix程序和Chrome作为主浏览器。 Any suggestions? 有什么建议么? Thank you in advance. 先感谢您。

That is because session_destroy(); 那是因为session_destroy(); destroys the current session and also sends a header to the browser to delete the session variable. 销毁当前会话,并向浏览器发送标头以删除会话变量。 In the same time the session is deleted on the server (in PHP) and the $_SESSION variables can no longer be used. 同时,在服务器上删除会话(在PHP中),并且不能再使用$ _SESSION变量。 You can always try to save the $_SESSION in another variable; 您总是可以尝试将$ _SESSION保存在另一个变量中;

session_start();
$_SESSION['test'] = 'foo';

Next page: 下一页:

session_start();
$saveSession = $_SESSION;
session_destroy();
var_dump($_SESSION); //Gives an empty array
var_dump($saveSession); //Still has ['test' => 'foo']

More information: http://php.net/manual/en/function.session-destroy.php and http://php.net/manual/en/book.session.php 详细信息: http : //php.net/manual/en/function.session-destroy.phphttp://php.net/manual/en/book.session.php

Also, sidenote, you do not need to open and close PHP tags if they are combined; 另外,请注意,如果将PHP标记组合在一起,则无需打开和关闭它们。

  <?php
  session_start();
  echo $_SESSION["name"];
  ?>

works just as well as 效果和

  <?php
  session_start();
  ?>
  <?php
  echo $_SESSION["name"];
  ?>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM