undo_magic_quotes_gpc()

Gepost door l0c4lh0st (127.0.0.1) op 01-04-2009 10:59.

Zet dit stukje code in elk script dat je gebruikt. Zorg wel dat het maximaal en minimaal 1 keer word uitgevoerd.

Bestanden van dit script

index.php

1
2
3
4
5
6
7
8
9
10
11
12
13
14
<?php
if (get_magic_quotes_gpc() == 1)
{
    function undo_magic_quotes_gpc($pmVariable)
    {
        return ((is_array($pmVariable)) ? array_map('undo_magic_quotes_gpc', $pmVariable) : stripslashes($pmVariable));
    }
    
    $_GET = undo_magic_quotes_gpc($_GET);
    $_POST = undo_magic_quotes_gpc($_POST);
    $_COOKIE = undo_magic_quotes_gpc($_COOKIE);
    $_REQUEST = undo_magic_quotes_gpc($_REQUEST);
}
?>

Commentaar

17-12-2005 11:01

Opzich natuurlijk handig, al staat in dat andere script -als je de comments bij elkaar raapt- hetzelfde.

Wat ik wel zou doen is je logica iets omdraaien, zodat de functie alleen uitgevoerd wordt als gpc = on.
Nu gooi je bij elke request opnieuw die functie er overheen, terwijl dat helemaal niet nodig is als gpc = off

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
17-12-2005 12:54

Zo wou ik het eerst doen... Ik kreeg op mijn eigen server geen error, maar op lycos.nl wel, dus ik nam aan dat het niet helemaal geldig PHP was...
Dat zou nl. beter zijn geweest, denk ik, bovenstaande.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
<?php
function undo_magic_quotes_gpc($mGetPostCookieRequestVariable = null)
{
    if (get_magic_quotes_gpc() == 1)
    {
        if (is_null($mGetPostCookieRequestVariable))
        {
            undo_magic_quotes_gpc($_POST);
            undo_magic_quotes_gpc($_GET);
            undo_magic_quotes_gpc($_COOKIE);
            undo_magic_quotes_gpc($_REQUEST);
        }
        else
        {
            if (is_array($mGetPostCookieRequestVariable))
            {
                return array_map('undo_magic_quotes_gpc', $mGetPostCookieRequestVariable);
            }
            else
            {
                return stripslashes($mGetPostCookieRequestVariable);
            }
        }
    }
    else
    {
        return $mGetPostCookieRequestVariable
    }
}

undo_magic_quotes_gpc();
?>
06-09-2006 08:22

Vermeld hier alle zaken betreffende valkuilen, handige informatie, (installatie-)instructies e.d.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
Mijn functie werkt als volgt:

<?php
if (get_magic_quotes_gpc ())
{
    function strip_deep ($a)
    {
        return is_array ($a)
            ? array_map ('strip_deep', $a)
            : stripslashes ($a);
    }

    $_POST = strip_deep ($_POST);
    $_GET  = strip_deep ($_GET);
    // let op: $_COOKIE kan nooit arrays bevatten
    $_COOKIE = strip_deep ($_COOKIE);
}
// en hier magic_quotes_runtime uitzetten
set_magic_quotes_runtime (0);
?>
06-09-2006 09:18

$_COOKIE kan wel degelijk arrays bevatten.

1
2
3
<?php
setcookie('hoi[test]', 'blaat', time() + 3600);
?>
15-04-2008 11:24

"$_COOKIE kan wel degelijk arrays bevatten.
Re: undo_magic_quotes_gpc()
<?php
setcookie('hoi[test]', 'blaat', time() + 3600);
?> "

strings alleen .

verder niet echt interessant om in de lib te zetten denk ik

1
<? echo 'zzzz'; ?>
15-04-2008 13:37

Het nut hiervan snap ik absoluut niet!
Je kan ze veel makkelijker in htaccess uitzetten!

<IfModule mod_php5.c>
php_flag magic_quotes_gpc off
</IfModule>

Daarnaast vind ik het een matig voorbeeld van recursie binnen php.

- Waarom zo'n lange $vanNaamBinnenEenFunctie. Nergens voor nodig IMO.
- d.m.v. een static kan je de magic quotes check gemakkelijk intern in de functie verwerken.
- __FUNCTION__ is hier flexibeler.

En dan nog heb je ook nog zoiets als:

http://www.php.net/manual/en/function.filter-input-array.php in combinatie met FILTER_SANITIZE_MAGIC_QUOTES...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<?php
function unquote($data){
    static $on = NULL;
    if(!isset($on)){
        $on = ini_get('magic_quotes_gpc');
    }
    return ((is_array($data)) ? array_map(__FUNCTION__, $data) : (($on) ? stripslashes($data) : $data));
}

//EDIT: zo issie misschien nog netter:
function unquote($data){
    static $on = NULL;
    if(!isset($on))    $on = ini_get('magic_quotes_gpc');
    return ((is_array($data)) ? array_map(__FUNCTION__, $data) : ((!!!$on) ? stripslashes($data) : $data));
}
?>
16-04-2008 19:28

In PHP.ini uitzetten is inderdaad de beste oplossing.
Als dat niet kan inderdaad in .htaccess.

Maar als ook dat niet gaat biedt dit script een oplossing.

Daarnaast is het gewoon vereist dat je dit aanroept, want er zijn genoeg kneusjes die een script downloaden en hier niets vanaf weten.

De filter-extensie is misschien wel leuk, maar magic-quotes gaan er gelukkig uit in PHP 6. Dat houdt wel in dat je bij een PHP upgrade je je scripts weer mag gaan updaten... Hier niet: Je zet er if (function_exists('get_magic_quotes_gpc')) omheen en klaar ben je.

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
13-06-2008 12:43

Kijk eens wat ik van php.net afhaal :
http://nl2.php.net/manual/en/security.magicquotes.disabling.php
En dan naar example #2

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<?php
if (get_magic_quotes_gpc()) {
    function stripslashes_deep($value)
    {
        $value = is_array($value) ?
                    array_map('stripslashes_deep', $value) :
                    stripslashes($value);

        return $value;
    }

    $_POST = array_map('stripslashes_deep', $_POST);
    $_GET = array_map('stripslashes_deep', $_GET);
    $_COOKIE = array_map('stripslashes_deep', $_COOKIE);
    $_REQUEST = array_map('stripslashes_deep', $_REQUEST);
}
?>
01-04-2009 11:13

Bij een test (een tijd geleden) met php6 kwam ik erachter dat de functie get_magic_quotes_gpc() er ook uit gaat.
Misschien dus eerst een function_exist of een ini check?

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
01-04-2009 14:19

Volgens mij gaan de hele magic quotes eruit, dus heb je dit niet eens nodig in php 6...

1
2
3
Post hier de source-code van je script. Alle informatie tussen <? ... ?> en <?php ... ?> zal automatisch worden getoond in color-coding. 

Let op! Het is niet de bedoeling om hier een link naar je website te plaatsen. Post hier gewoon de code, veel simpeler, sneller en meer kans dat het blijft staan.
01-04-2009 15:32

Misschien is het handig om fouten te voorkomen dat je een variabel/constante laat zetten als je de slashes verwijdert hebt. En dan ook nog aan het begin controleren of dat is gezet (oid)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php 
if (get_magic_quotes_gpc() == 1) { 
    if(!defined('UNDO_GPC')) {
        function undo_magic_quotes_gpc($pmVariable) { 
            return ((is_array($pmVariable)) ? array_map('undo_magic_quotes_gpc', $pmVariable) : stripslashes($pmVariable)); 
        } 
    
         $_GET = !empty($_GET) ? undo_magic_quotes_gpc($_GET) : ''; 
         $_POST = !empty($_POST) ? undo_magic_quotes_gpc($_POST) : '';  
         $_COOKIE = !empty($_COOKIE) ? undo_magic_quotes_gpc($_COOKIE) : ''; 
         $_REQUEST = !empty($_REQUEST) ? undo_magic_quotes_gpc($_REQUEST) : '';  
            define('UNDO_GPC', true);
    }
} 
?>
01-04-2009 22:55

Deze voorkomt ook dat het uitgevoerd wordt op PHP 6 of hoger.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
if(version_compare(PHP_VERSION, '6.0.0') === -1 && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() === '1' && defined('UNDO_GPC') === false)
{
    function undo_magic_quotes_gpc($aArray)
    {
        return is_array($aArray) ? array_map('undo_magic_quotes_gpc', $aArray) : stripslashes($aArray);
    }
    $_GET = undo_magic_quotes_gpc($_GET);
    $_POST = undo_magic_quotes_gpc($_POST);
    $_COOKIE = undo_magic_quotes_gpc($_COOKIE);
    $_FILES = undo_magic_quotes_gpc($_FILES);
    $_REQUEST = undo_magic_quotes_gpc($_REQUEST);
    define('UNDO_GPC', true);
}
?>
14-05-2009 22:10

" get_magic_quotes_gpc() === '1' "

get_magic_quotes_gpc() geeft altijd een integer terug en geen string. Hierdoor voldoe je nooit aan de voorwaarde en ga je nooit de if in.

Even de quotjes om de 1 weghalen, zodat er vergeleken wordt met een integer.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<?php
if(version_compare(PHP_VERSION, '6.0.0') === -1 && function_exists('get_magic_quotes_gpc') && get_magic_quotes_gpc() === 1 && defined('UNDO_GPC') === false)
{
    function undo_magic_quotes_gpc($aArray)
    {
        return is_array($aArray) ? array_map('undo_magic_quotes_gpc', $aArray) : stripslashes($aArray);
    }
    $_GET = undo_magic_quotes_gpc($_GET);
    $_POST = undo_magic_quotes_gpc($_POST);
    $_COOKIE = undo_magic_quotes_gpc($_COOKIE);
    $_FILES = undo_magic_quotes_gpc($_FILES);
    $_REQUEST = undo_magic_quotes_gpc($_REQUEST);
    define('UNDO_GPC', true);
}
?>
Inloggen wachtwoord vergeten? Aanmelden