Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP]Zabezpieczenie przed SQL Injection
robos85
post
Post #1





Grupa: Zarejestrowani
Postów: 466
Pomógł: 11
Dołączył: 21.09.2006
Skąd: Szczecin

Ostrzeżenie: (0%)
-----


Czy takie coś pozwoli mi się zabezpieczyć przed SQL Inj.. ?
  1. <?php
  2. function sqlesc($x) {
  3.    return mysql_real_escape_string($x);
  4. }
  5.  
  6. function strip_magic_quotes($arr)
  7. {
  8.    foreach ($arr as $k => $v){
  9.        if (is_array($v)){
  10.            $arr[$k] = strip_magic_quotes($v);
  11.        }
  12.        else{
  13.            $arr[$k] = stripslashes($v);
  14.        }
  15.    }    
  16.    return $arr;
  17. }
  18.  
  19. {
  20.    if (!empty($_GET)) { $_GET = strip_magic_quotes($_GET); }
  21.    if (!empty($_POST)) { $_POST = strip_magic_quotes($_POST); }
  22.    if (!empty($_COOKIE)) { $_COOKIE = strip_magic_quotes($_COOKIE); }
  23. }
  24.  
  25.  
  26. // addslashes to vars if magic_quotes_gpc is off
  27. // this is a security precaution to prevent someone
  28. // trying to break out of a SQL statement.
  29. //
  30.  
  31. {
  32.    if( is_array($_GET) ){
  33.        while( list($k, $v) = each($_GET) ) {
  34.            if( is_array($_GET[$k]) ){
  35.                while( list($k2, $v2) = each($_GET[$k]) ){
  36.                    $_GET[$k][$k2] = addslashes($v2);
  37.                }
  38.                @reset($_GET[$k]);
  39.            }
  40.            else {
  41.                $_GET[$k] = addslashes($v);
  42.            }
  43.        }
  44.        @reset($_GET);
  45.    }
  46.    
  47.    if( is_array($_POST) ){
  48.        while( list($k, $v) = each($_POST) ){
  49.            if( is_array($_POST[$k]) ){
  50.                while( list($k2, $v2) = each($_POST[$k]) ){
  51.                    $_POST[$k][$k2] = addslashes($v2);
  52.                }
  53.                @reset($_POST[$k]);
  54.            }
  55.            else{
  56.                $_POST[$k] = addslashes($v);
  57.            }
  58.        }
  59.        @reset($_POST);
  60.    }
  61.    
  62.    if( is_array($_COOKIE) ){
  63.        while( list($k, $v) = each($_COOKIE) ){
  64.            if( is_array($_COOKIE[$k]) ){
  65.                while( list($k2, $v2) = each($_COOKIE[$k]) ){
  66.                    $_COOKIE[$k][$k2] = addslashes($v2);
  67.                }
  68.                @reset($_COOKIE[$k]);
  69.            }
  70.            else{
  71.            $_COOKIE[$k] = addslashes($v);
  72.            }
  73.        }
  74.        @reset($_COOKIE);
  75.    }
  76. }
  77. ?>


a dodawanie/update do bazy robić tak:
  1. <?php
  2. "UPDATE ...... SET `cos`='".sqlesc($_GET['blabla']."'..."
  3. ?>


wyświetlenie wyników już na stronę z htmlspecialchars" title="Zobacz w manualu PHP" target="_manual

i jak, takie coś może być?

Ten post edytował robos85 30.09.2008, 13:33:51
Go to the top of the page
+Quote Post
skowron-line
post
Post #2





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

Ostrzeżenie: (0%)
-----


Na forum jest cały temat poświęcony SQL injection poszukaj i poczytaj a nie bedziesz zadawał więcej takich pytań


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post
MWL
post
Post #3





Grupa: Zarejestrowani
Postów: 493
Pomógł: 32
Dołączył: 14.04.2008
Skąd: Lenkowski.net

Ostrzeżenie: (0%)
-----


Zadziała, ale dajesz tego za dużo, wystarczy samo mysql_real_escape_string


--------------------
Wpadaj na mój kanał o PHP. Dużo mięsa 🥩!
Go to the top of the page
+Quote Post

Reply to this topicStart new topic
1 Użytkowników czyta ten temat (1 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 20.08.2025 - 06:18