I agree with Whetstone. get_defined_vars()
is just perfect for "multi-unset" tricks.
This method below, unsets ALL variables. All except those found within nameless array inside foreach loop.
foreach(get_defined_vars() as $k=>$y){
if( !in_array( $k,
array(
'myRequredVariableName1',
'myRequredVariableName2',
'_ENV',
'_SESSION',
'_COOKIE',
'HTTP_SESSION_VARS',
'HTTP_COOKIE_VARS'
)))
{ $$k=null; unset($$k);}
unset($y, $k);
}
// Check for leftovers
header('Content-type:text/plain; charset=utf-8');
var_export(get_defined_vars());
exit;
The values are actually variable names without '$', where variable variable unset($$k);
matches the REAL and defined one, and destroys it, if it is present. So, on this way, You can have complete control over what is left for system.
Note that some shared hosting setups are relying only on _SERVER superglobals, therefore _ENV, HTTP_SESSION_VARS, HTTP_COOKIE_VARS are not required at all. Most probably You'll always want to keep _COOKIE and _SESSION but not _GET and _POST as well or _FILES. The decision varies. Test and see for Your self what should stand within array before putting this trick on Your production environment.