如何正确清除我的 php 会话数据?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15492197/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 09:20:27  来源:igfitidea点击:

How can I clear my php session data correctly?

phpsession

提问by Wesley Smith

My php website flows like this:

我的 php 网站流程是这样的:

  • Page1.php has an html form which POSTs to Page2.php
  • Page2.php stores all the POST data into SESSION variables and has a button leading to Page3.php
  • Page3.php has another form which POSTs its data to Page4.php
  • Page4.php then stores all its POST data into SESSION variables
  • Page1.php 有一个 html 表单,它发布到 Page2.php
  • Page2.php 将所有 POST 数据存储到 SESSION 变量中,并有一个通向 Page3.php 的按钮
  • Page3.php 有另一种将数据发布到 Page4.php 的表单
  • Page4.php 然后将其所有 POST 数据存储到 SESSION 变量中

My problem is that it may be nessicary for a user to click the back button on Page4.php to go back to Page3.php and change some input. AS im sure your all aware when they get back to Page3.php the form will be blank as the entire page is re-rendered in its default state.

我的问题是用户可能需要单击 Page4.php 上的后退按钮返回 Page3.php 并更改一些输入。我相信你们都知道当他们回到 Page3.php 时,表单将是空白的,因为整个页面都在其默认状态下重新呈现。

To get around this and re-display the user's previous input im doing this:

为了解决这个问题并重新显示用户之前的输入,我这样做:

<input value="<?php echo $_POST["guest1Ticket"];?> " type="text"  name="guest1Ticket" id="guest1Ticket" onblur="isTicketNumber(this)"  size ="22"/>

This being the important part - <?php echo $_POST["guest1Ticket"];?>

这是重要的部分—— <?php echo $_POST["guest1Ticket"];?>

This works but creates another problem for me. If the user goes back to Page1.php (before colsing their browser) and starts the process over again when they get to Page3.php the data from their last run through will be loaded into the form.

这有效,但给我带来了另一个问题。如果用户返回 Page1.php(在关闭浏览器之前)并在他们到达 Page3.php 时重新开始该过程,则上次运行的数据将加载到表单中。

What I figure I need to do is clear all of the sdession variables when the user visists Page1.php. I tried to to that like this:

我认为我需要做的是在用户访问 Page1.php 时清除所有 sdession 变量。我试图这样做:

<?php 
 session_start(); 
 session_unset(); 
 session_destroy(); 
?> 

(The above is at the very top of my file with no whitespace before the first character.)

(上面是我文件的最顶部,第一个字符前没有空格。)

No Warnings are generated when Page1.php loads but the session variables are not getting unset. When I get to Page3.php the data from the last run is still being entered into the form.

当 Page1.php 加载但会话变量没有取消设置时,不会生成警告。当我到达 Page3.php 时,上次运行的数据仍在输入表单中。

How can I clear my session data correctly?

如何正确清除会话数据?

BTW I only need this to work in Chromeand thats where im testing.

顺便说一句,我只需要它在 Chrome 中工作,这就是我测试的地方。

回答by Grmn

Only use session_unset() for older deprecated code that does not use $_SESSION.

仅对不使用 $_SESSION 的旧弃用代码使用 session_unset()。

see session_destroy manual

参见session_destroy 手册

example you can try and see how it works

例如你可以尝试看看它是如何工作的

session.php

会话文件

<?php
session_start();
$_SESSION = array('session1'=>1,'session2'=>2);

echo $_SESSION['session1']; //1
$_SESSION['session1'] = 3;
echo "<pre>";
print_r($_SESSION); //session one now updated to 3
echo "</pre>";


$_SESSION = array();
if ($_SESSION['session1']) {
 echo $_SESSION['session1']; //  IS NOW EMPTY
} else {
 echo "woops... nothing found";
}
?>
<p>
<a href="destroyed.php">NOW GOING TO DESTROYED PHP<a/>
</p>

<?php
session_destroy();
?>

destroyed.php

销毁.php

<?php
session_start(); // calling session start first on destroyed.php

print_r($_SESSION); // prints Array ( )  
?>

回答by MatRt

From the documentation:

从文档:

If $_SESSION (or $HTTP_SESSION_VARS for PHP 4.0.6 or less) is used,
use unset() to unregister a session variable, i.e. unset ($_SESSION['varname']);

And take care about session_destroy:

并注意 session_destroy:

session_destroy destroys all of the data associated with the current session. It does not unset any of the global variables associated with the session

session_destroy 销毁与当前会话相关的所有数据。它不会取消设置与会话关联的任何全局变量

回答by Ewer Ling

Use session_unset(). Like this:

使用 session_unset()。像这样:

<?php session_start(); ?><!DOCTYPE html>
<html>
  <body>
    <?php
      $_SESSION["variabletounset"] = "I am going to be unset soon along with all of the other session variables.";
      print '<pre>' . "\n";
      print_r($_SESSION);
      print '    </pre>' . "\n";

      session_unset();

      print '    <pre>' . "\n";
      print_r($_SESSION);
      print '    </pre>' . "\n";
    ?>
  </body>
</html>

This would output:

这将输出:

Array
(
variabletounset => I am going to be unset soon along with all of the other session variables.
)

Array
(
)