Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [PHP]Zabezpieczenie przed SQL Injection
Forum PHP.pl > Forum > Przedszkole
robos85
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ć?
skowron-line
Na forum jest cały temat poświęcony SQL injection poszukaj i poczytaj a nie bedziesz zadawał więcej takich pytań
MWL
Zadziała, ale dajesz tego za dużo, wystarczy samo mysql_real_escape_string
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.