note 35321 deleted from function.get-magic-quotes-gpc by aidan
| From: | aidan@php.net | Date: | Tue, 06 Jul 2004 06:20:14 +0000 |
| Subject: | note 35321 deleted from function.get-magic-quotes-gpc by aidan | ||
| References: | 1 | Groups: | php.notes |
| Request: | Send a blank email to php-notes+get-72695@lists.php.net to get a copy of this message | ||
Note Submitter: davidbaker84@hotmail.com
----
Here's yet another way of making code that will work regardless of whether gpc_magic_quotes is
on or off:
<?php
if (get_magic_quotes_gpc()) {
function maybeaddslashes(&$inthing) { }
function maybestripslashes(&$inthing) {
if (is_array($inthing)) {
foreach ($inthing as $element) {
$element = stripslashes($element);
}
} else {
$inthing = stripslashes($inthing);
}
}
} else {
function maybeaddslashes(&$inthing) {
if (is_array($inthing)) {
foreach ($inthing as $element) {
$element = addslashes($element);
}
} else {
$inthing = addslashes($inthing);
}
}
function maybestripslashes(&$inthing) {}
}
?>
You can call maybeaddslashes() before adding performing MySQL queries, or the like, and
maybestripslashes() when you don't want the slashes in your GPC data.
It also has the advantage that it makes the decision only once, so theoretically is slighly faster.
Oh, as you can see, it will convert arrays of strings too.