WordPress and PHP magic quotes: you want to run me crazy!
Not understanding how WordPress manages the magic quotes of PHP (PHP is no more supporting magic quotes gpc from version 6, and when this version will be released we see a lot of sites hacked…) I followed the WordPress startup code.
The code is all in wp-settings.php. These my conclusions:
- $_REQUEST is redefined as a pure merge of $_GET and $_POST and can have or not magic quotes, to use it you need to check the “magic_quote_gpc()” function
- $_GET, $_POST, $_COOKIE and $_SERVER arrays are forced with escaped values so to have the real values you need to strip the slashes
The code below produces such effect:
if ( get_magic_quotes_gpc() ) {
$_GET = stripslashes_deep($_GET );
$_POST = stripslashes_deep($_POST );
$_COOKIE = stripslashes_deep($_COOKIE);
}// Escape with wpdb.
$_GET = add_magic_quotes($_GET );
$_POST = add_magic_quotes($_POST );
$_COOKIE = add_magic_quotes($_COOKIE);
$_SERVER = add_magic_quotes($_SERVER);
$_REQUEST is created before with:
$_REQUEST = array_merge($_GET, $_POST);
so we cannot be sure if it is escaped or not.
To strip slashes, WordPress has a function “stripslashes_deep($value)” which manages array type values.
Hence, to extract a POST or a GET parameter we have to write:
$value = stripslashes_deep($_POST['name']); or
$value = stripslashes_deep($_GET['name']);
The same thing when extractin cookie or server values.
Is that a definitive answer??? (if so my plugins need an update… I’ll start from Dynatags…)
I'm Stefano Lissa. 10 years ago I was building web sites for my pleasure. Blogs didn't exist, web content systems were ugly, hosting really expensive. And my student pocket was empty.
Thanks for the article.
I’d been wandering around the Codex site wondering why the hell slashes were getting added to all of my request data when I always work without magic_quotes on.
I don’t even understand why this is happening – as even with adding slashes, SQL injection can still occur if the queries are not escaped with proper SQL escape functions.
All this can really lead to is bad programming practice for the newer plugin developers as they will see data ‘escaped’ automatically and assume that it’s OK.
I think wordpress guys decided to work this way to uniform the request format ove the so many php configuration worldwide.
From php 6 magic quotes Are deprecate as i know