Skip to content

Instantly share code, notes, and snippets.

@pestilence669
Last active August 29, 2015 14:03
Show Gist options
  • Save pestilence669/1329d4695e29604212cd to your computer and use it in GitHub Desktop.
Save pestilence669/1329d4695e29604212cd to your computer and use it in GitHub Desktop.
Old code I posted in the comments of php.net years ago. It's boilerplate code for automating the handling of PHP's "magic" quoting behavior. It was copy and pasted frequently, after I renamed it "unfck_gpc" instead of "unfuck_gpc," which was deleted as offensive. As far as I can tell, the only big project that still uses it, is a Russian CMS.
<?php
// vim: set ts=4 sw=4 noet:
/*
In the example above, the author forgets to include $_REQUEST,
which is also slashed (using PHP 4.3.8).
It's good practice to include a routine to "unslash" or "slash"
variables, if something happens that isn't to your expectation.
However your PHP is written to depend on this option, it becomes
important to support either when or if others deploy your code.
---------------------------------------------------------------------
If you have written for having this option disabled:
---------------------------------------------------------------------
*/
if (get_magic_quotes_gpc()) unfck_gpc();
function unfck($v) {
return is_array($v) ? array_map('unfck', $v) : stripslashes($v);
}
function unfck_gpc() {
foreach (array('POST', 'GET', 'REQUEST', 'COOKIE') as $gpc)
$GLOBALS["_$gpc"] = array_map('unfck', $GLOBALS["_$gpc"]);
}
/*
---------------------------------------------------------------------
If you have written for having this option enabled:
---------------------------------------------------------------------
*/
if (!get_magic_quotes_gpc()) unfck_gpc();
function unfck($v) {
return is_array($v) ? array_map('unfck', $v) : addslashes($v);
}
function unfck_gpc() {
foreach (array('POST', 'GET', 'REQUEST', 'COOKIE') as $gpc)
$GLOBALS["_$gpc"] = array_map('unfck', $GLOBALS["_$gpc"]);
}
/*
---------------------------------------------------------------------
Including either code block, depending on how your PHP is written,
will ensure that it will operate the same regardless of this
configuration option.
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment