php 完全清除 _POST 数组

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12953917/
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 04:32:28  来源:igfitidea点击:

Clearing _POST array fully

php

提问by Oto Shavadze

I want clear $_POST array content fully, all examples what I see in internet, looks like this:

我想要完全清除 $_POST 数组内容,我在互联网上看到的所有示例都如下所示:

if (count($_POST) > 0) {
    foreach ($_POST as $k=>$v) {
        unset($_POST[$k]);
    }
}

Tell please, this variant will be not more better? (Point of view as saving resources)

请告诉,这个变种会不会更好?(节约资源的观点)

if (count($_POST) > 0) {
     $_POST = array();
}

or not ?

或不 ?

回答by Wesley Murch

Yes, that is fine. $_POSTis just another variable, except it has (super)global scope.

是的,那很好。$_POST只是另一个变量,除了它具有(超级)全局范围。

$_POST = array();

...will be quite enough. The loop is useless. It's probably best to keep it as an array rather than unset it, in case other files are attempting to read it and assuming it is an array.

……就够了。循环没用。最好将它保留为数组而不是取消设置,以防其他文件尝试读取它并假设它是一个数组。

回答by rationalboss

To unset the $_POSTvariable, redeclare it as an empty array:

要取消设置$_POST变量,请将其重新声明为空数组:

$_POST = array();

回答by kiwicomb123

The solutions so far don't work because the POST data is stored in the headers. A redirect solves this issue according this this post.

到目前为止的解决方案不起作用,因为 POST 数据存储在标头中。重定向根据这篇文章解决了这个问题。

How to delete $_POST variable upon pressing 'Refresh' button on browser with PHP?

如何在使用 PHP 的浏览器上按下“刷新”按钮时删除 $_POST 变量?

回答by Bill

It may appear to be overly awkward, but you're probably better off unsetting one element at a time rather than the entire $_POST array. Here's why: If you're using object-oriented programming, you may have one class use $_POST['alpha'] and another class use $_POST['beta'], and if you unset the array after first use, it will void its use in other classes. To be safe and not shoot yourself in the foot, just drop in a little method that will unset the elements that you've just used: For example:

这可能看起来过于笨拙,但最好一次取消设置一个元素,而不是整个 $_POST 数组。原因如下:如果您使用面向对象编程,您可能有一个类使用 $_POST['alpha'] 而另一个类使用 $_POST['beta'],如果您在第一次使用后取消设置数组,它将使其在其他类中的使用无效。为了安全起见,不要用脚射击自己,只需使用一个小方法来取消您刚刚使用的元素:例如:

private function doUnset()
{
    unset($_POST['alpha']);
    unset($_POST['gamma']);
    unset($_POST['delta']);
    unset($_GET['eta']);
    unset($_GET['zeta']);
}

Just call the method and unset just those superglobal elements that have been passed to a variable or argument. Then, the other classes that may need a superglobal element can still use them.

只需调用该方法并取消设置那些已传递给变量或参数的超全局元素。然后,可能需要超全局元素的其他类仍然可以使用它们。

However, you are wise to unset the superglobals as soon as they have been passed to an encapsulated object.

但是,明智的做法是在将超全局变量传递给封装对象后立即取消设置它们。

回答by Praveen Kumar Purushothaman

You can use a combination of both unset()and initialization:

您可以结合使用unset()和初始化:

unset($_POST);
$_POST = array();

Or in a single statement:

或者在一个声明中:

unset($_POST) ? $_POST = array() : $_POST = array();

But what is the reason you want to do this?

但是你想这样做的原因是什么?

回答by Damir Olejar

To answer "why" someone might use it, I was tempted to use it since I had the $_POST values stored after the page refresh or while going from one page to another. My sense tells me this is not a good practice, but it works nevertheless.

为了回答“为什么”有人可能会使用它,我很想使用它,因为我在页面刷新后或从一个页面转到另一个页面时存储了 $_POST 值。我的感觉告诉我这不是一个好的做法,但它仍然有效。