Goed slashes weghalen / plaatsen

Gestart doorRik15 reacties77 weergaven

16 berichten · pagina 1 van 2

Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Dit vraag ik me altijd al af =]

Je hebt bijvoorbeeld deze zin:

het " plaatsen  van  deze  zin  " in \ een database  gaat    moeilijk.


Echter als je gaat escapen en daarna echo-en wordt het dit:

het " plaatsen  van \ deze \ zin  " in \\ een database  gaat  \  moeilijk.


Of zoiets.

Maar hoe krijg je dit veilig in de database EN goed weergegeven (gewoon zoals het ingevoerd wordt?

Alvast bedankt smile
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Zorgen dat dit altijd wordt geïnclude of iets:
[COLOR= #0000BB]<?php[/COLOR]
[COLOR= #007700]function[/COLOR] strip[COLOR= #007700]([/COLOR][COLOR= #0000BB]$string[/COLOR][COLOR= #007700])
{[/COLOR]
    
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$string[/COLOR][COLOR= #007700]))
    {[/COLOR]
        
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/get_magic_quotes_gpc"]get_magic_quotes_gpc[/URL][COLOR= #007700]() ==[/COLOR] [COLOR= #0000BB]1[/COLOR] [COLOR= #007700]?[/COLOR] [URL="http://www.php.net/stripslashes"]stripslashes[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$string[/COLOR][COLOR= #007700]) :[/COLOR] [COLOR= #0000BB]$string[/COLOR][COLOR= #007700];
    
    }[/COLOR]
    [URL="http://www.php.net/else"]else[/URL]
    [COLOR= #007700]{[/COLOR]
        
        [COLOR= #0000BB]$nieuwe[/COLOR] [COLOR= #007700]=[/COLOR] [URL="http://www.php.net/array"]array[/URL][COLOR= #007700]();[/COLOR]
        
        [URL="http://www.php.net/foreach"]foreach[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$string[/COLOR] [COLOR= #007700]as[/COLOR] [COLOR= #0000BB]$key[/COLOR] [COLOR= #007700]=>[/COLOR] [COLOR= #0000BB]$value[/COLOR][COLOR= #007700])
        {[/COLOR]
            
            [URL="http://www.php.net/if"]if[/URL][COLOR= #007700](![/COLOR][URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]))
            {[/COLOR]
            
                [COLOR= #0000BB]$nieuwe[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$key[/COLOR][COLOR= #007700]] =[/COLOR] [URL="http://www.php.net/get_magic_quotes_gpc"]get_magic_quotes_gpc[/URL][COLOR= #007700]() ==[/COLOR] [COLOR= #0000BB]1[/COLOR] [COLOR= #007700]?[/COLOR] [URL="http://www.php.net/stripslashes"]stripslashes[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]) :[/COLOR] [COLOR= #0000BB]$value[/COLOR][COLOR= #007700];
            
            }[/COLOR]
            [URL="http://www.php.net/else"]else[/URL]
            [COLOR= #007700]{[/COLOR]
                
                [COLOR= #0000BB]$nieuwe[/COLOR][COLOR= #007700][[/COLOR][COLOR= #0000BB]$key[/COLOR][COLOR= #007700]] =[/COLOR] strip[COLOR= #007700]([/COLOR][COLOR= #0000BB]$value[/COLOR][COLOR= #007700]);
            
            }
        
        }[/COLOR]
        
        [URL="http://www.php.net/return"]return[/URL] [COLOR= #0000BB]$nieuwe[/COLOR][COLOR= #007700];
    
    }
    
}[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])[/COLOR]
    [COLOR= #0000BB]$_GET[/COLOR] [COLOR= #007700]=[/COLOR] strip[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]);[/COLOR]

[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])[/COLOR]
    [COLOR= #0000BB]$_POST[/COLOR] [COLOR= #007700]=[/COLOR] strip[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]);[/COLOR]
?>


Om hem korter te maken kun je hem herschrijven met array_map(), maar dat moet je zelf maar doen.
Bij het in de database zetten of eruit halen heb je verder geen addslashes of stripslashes meer nodig.
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Dus als ik dit gebruik moet ik bij het invoegen EN weergeven Strip() doen ? of aleen invoegen?
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Nee, gewoon bovenaan je script zetten, verder er niks mee doen.
Hij haalt automatisch alle slashes uit $_POST en $_GET als dat nodig is, omdat magic quotes ervoor zorgt dat alles wordt geëscaped, en dat wil je niet.

edit:
en er voor zorgen dat het max 1 keer wordt uitgevoerd, anders krijg je gare situaties
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Ik zal eens kieken yummie
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Zo issie korter, maar weet niet offie goed werkt:
[COLOR= #0000BB]<?php[/COLOR]
[URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/get_magic_quotes_gpc"]get_magic_quotes_gpc[/URL][COLOR= #007700]() ==[/COLOR] [COLOR= #0000BB]1[/COLOR][COLOR= #007700])
{
    
    function[/COLOR] strip[COLOR= #007700]([/COLOR][COLOR= #0000BB]$var[/COLOR][COLOR= #007700])
    {[/COLOR]
        
        [URL="http://www.php.net/return"]return[/URL] [URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var[/COLOR][COLOR= #007700]) ?[/COLOR] [URL="http://www.php.net/array_map"]array_map[/URL][COLOR= #007700]([/COLOR][COLOR= #DD0000]stripslashes[/COLOR][COLOR= #007700],[/COLOR] [COLOR= #0000BB]$var[/COLOR][COLOR= #007700]) :[/COLOR] [URL="http://www.php.net/stripslashes"]stripslashes[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$var[/COLOR][COLOR= #007700]);
        
    }[/COLOR]
    
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])[/COLOR]
        [COLOR= #0000BB]$_GET[/COLOR] [COLOR= #007700]=[/COLOR] strip[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_GET[/COLOR][COLOR= #007700]);[/COLOR]
    
    [URL="http://www.php.net/if"]if[/URL][COLOR= #007700]([/COLOR][URL="http://www.php.net/isset"]isset[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/is_array"]is_array[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]) &&[/COLOR] [URL="http://www.php.net/count"]count[/URL][COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]) >[/COLOR] [COLOR= #0000BB]0[/COLOR][COLOR= #007700])[/COLOR]
        [COLOR= #0000BB]$_POST[/COLOR] [COLOR= #007700]=[/COLOR] strip[COLOR= #007700]([/COLOR][COLOR= #0000BB]$_POST[/COLOR][COLOR= #007700]);    
        
}[/COLOR]
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Ik zal ze alebei na het eten testen, en kijken welke werkt smile
Danke schön
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Prima. Die eerste werkt sowieso, die hebben we op o2x hierzo.
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Heb het getest, bij het echo-en krijg ik dit :

1) het " plaatsen van deze zin " in een database gaat moeilijk.
2) het " plaatsen van deze zin " in een database gaat moeilijk.

Real_escape:

1) het " plaatsen van deze zin " in een database gaat moeilijk.

Dus zal wel werken allebei biggrin
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Je moet het bij het naar de database wegschrijven alsnog mysql_real_escape_string() gebruiken he.
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Okie =D

heb hem nu geimplanteerd, en in het script zelf :

/* slash-stripper is gemaakt door Lennard van one2xs.com / one2apps.com */

Gezet smile
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Dankje. emo
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Naja, vind het altijd zo eerlijk de eigenaar van het script op een of andere manier terug te laten komen, is wel zo netjes wink
0
Berichten
35.796
Reputatie
1
Punten
0,00
Lid sinds
Laatst gezien
Ach, t maakt mij weinig uit, er zien toch weinig mensen de broncode van je site lijkt me. emo
0
Rik

Lid · Topicstarter

Berichten
18.393
Reputatie
0
Punten
0,04
Lid sinds
Laatst gezien
Niet dat ik weet nee, aleen mijn mede-beheerder. Maar als ik eventueel later nog scripts vrij ga geven waarin dit script komt (zeer waarschijnlijk, of een soortgelijk script, omdat het erg handig is) dan weet ik nog van wie het is emo
0

Jouw reactie

Deel je idee, stel een vraag of help een ander verder.

Editor laden…

Praat mee met de One2xs-community.

Inloggen om te reageren