Clearing _POST array fully

前端 未结 6 1002
面向向阳花
面向向阳花 2020-12-05 10:17

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

if (count($_POST) > 0) {
    foreach ($_POST as $k=>$v)          


        
6条回答
  •  孤街浪徒
    2020-12-05 10:28

    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:

    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.

提交回复
热议问题