The best answer provided here is to copy for own use like:
$post = array_map('stripslashes_deep', $_POST);
There's a theoretical problem with this however: since you're working with a duplicate, you can't persist any changes to the superglobals (hey, I'm not saying it's a good practice, alright?).
Solution: accessor methods
In an attempt to solve this mess in a definite manner and without any side effects, I made "accessor methods" which transparently apply stripslashes_deep()
or addslashes_deep()*
to get/set requests to the following superglobal arrays:
* I had to throw addslashes_deep()
together from WordPress' stripslashes_deep()
.
$_GET
$_POST
$_COOKIE
$_SERVER
$_REQUEST
You can use them like:
echo _get('username'); // echo stripslashes_deep($_GET['username']);
_cookie('name', 'value'); // $_COOKIE['name'] = addslashes_deep('value');
Here's the code (I call it gpcsr.php
):
<?php
// cat stripslashes_deep() | sed 's/stripslashes/addslashes/g'
function addslashes_deep( $value ) {
if ( is_array($value) ) {
$value = array_map('addslashes_deep', $value);
} elseif ( is_object($value) ) {
$vars = get_object_vars( $value );
foreach ($vars as $key=>$data) {
$value->{$key} = addslashes_deep( $data );
}
} elseif ( is_string( $value ) ) {
$value = addslashes($value);
}
return $value;
}
function _generic_slashes_wrap(&$arr, $key, $value = null) {
if (func_num_args() === 2) return stripslashes_deep($arr[$key]);
else $arr[$key] = addslashes_deep($value);
}
function _get ($key, $value = null) { if (func_num_args() === 1) return _generic_slashes_wrap($_GET, $key); else _generic_slashes_wrap($_GET, $key, $value); }
function _post ($key, $value = null) { if (func_num_args() === 1) return _generic_slashes_wrap($_POST, $key); else _generic_slashes_wrap($_POST, $key, $value); }
function _cookie ($key, $value = null) { if (func_num_args() === 1) return _generic_slashes_wrap($_COOKIE, $key); else _generic_slashes_wrap($_COOKIE, $key, $value); }
function _server ($key, $value = null) { if (func_num_args() === 1) return _generic_slashes_wrap($_SERVER, $key); else _generic_slashes_wrap($_SERVER, $key, $value); }
function _request ($key, $value = null) { if (func_num_args() === 1) return _generic_slashes_wrap($_REQUEST, $key); else _generic_slashes_wrap($_REQUEST, $key, $value); }
?>