Brandon Slater
Brandon Slater

Reputation: 33

Unsetting Superglobal Variable Properly

I've recently switched from Dreamweaver to Netbeans and, with the built-in accountability for clean code, I've realized I have some work to do in writing proper/clean code.

Currently, I'm trying to adjust from accessing SuperGlobals (such as $_POST) directly. For this question, specifically, I am trying to figure out how to handle unsetting a SuperGlobal variable temporarily.

$sampleArray = ["Designer_Name" => "someName",
            "Designer_Key" => "someName's UID",
            "Designer_Store" => "someStoreName",
            "Group_Name" => "someGroupName",
            "Dropbox_Key" => "someUID"
            ];

Normally, I might do something like this:

unset($_POST['Dropbox_Key']);

In this particular problem, I am trying to use short amounts of code. I know that I can filter/sanitize each element of the array and assign it to a variable, but since the $_POST array is not user-input, I'm not too concerned with that.

With this array, I'll be inserting the entire array into a database table, minus the last element. I don't need 'Dropbox_Key' for the same table as the rest of the array. I will use that element later for another table.

The insert code would look like this:

$insert = $dbs->insert('sometable', filter_input_array(INPUT_POST));

So, the question I am asking is, despite it being easier to ignore the netbeans warnings of accessing SuperGlobals, is there some current/cleaner way to do what I intend (the unsetting of the $_POST element), or is ignoring the warning the best way to go here?

Upvotes: 2

Views: 1098

Answers (1)

Jason Joslin
Jason Joslin

Reputation: 1144

The reason we use filter_input() on super global input's like $_POST is because it tends to be user entered data we cannot trust. By filtering we can escape and sanitize the inputs to help prevent XSS (cross-site scripting) and SQLinjections.

In the case of un-setting super global's in your own code there is no risk posed (You are in control of your code), so we can safely unset directly unset($_POST['Dropbox_Key']);

Upvotes: 1

Related Questions