Visualizzazione dei risultati da 1 a 4 su 4
  1. #1

    [mysql] Evitare sql injection per i nomi delle tabelle.

    Ho una variabile php che contiene il nome della tabella da cui prendere i dati:
    codice:
    "SELECT * FROM `$table` WHERE campo='valorequalsiasi';"
    Questa variabile non la creo io, al contrario è contenuta nell'url.
    La domanda quindi è: come faccio ad evitare una sql injection?
    Secondo voi sono al sicuro se mi creo una funzione che verifica che il nome della tabella contenga solo numeri, lettere e i caratteri - e _ ? Oppure per eludere tale controllo basterebbe la notazione esadecimale?

  2. #2
    Utente di HTML.it L'avatar di carlo2002
    Registrato dal
    Jun 2002
    Messaggi
    2,746
    Potresti anche fare una funzione che verifica che il nome della tabella esista realmente nel database.

    Errare humanum est, perseverare ovest

  3. #3
    Grazie. Se a qualcuno può essere utile, si può fare così:
    Codice PHP:
    function TableExists($tablename$db) {
        
    // Get a list of tables contained within the database.
        
    $result mysql_list_tables($db);
        
    $rcount mysql_num_rows($result);

        
    // Check each in list for a match.
        
    for ($i=0;$i<$rcount;$i++) {
            if (
    mysql_tablename($result$i)==$tablename) return true;
        }
        return 
    false;


  4. #4
    Utente di HTML.it L'avatar di dottwatson
    Registrato dal
    Feb 2007
    Messaggi
    3,012
    Originariamente inviato da Mega69
    Grazie. Se a qualcuno può essere utile, si può fare così:
    Codice PHP:
    function TableExists($tablename$db) {
        
    // Get a list of tables contained within the database.
        
    $result mysql_list_tables($db);
        
    $rcount mysql_num_rows($result);

        
    // Check each in list for a match.
        
    for ($i=0;$i<$rcount;$i++) {
            if (
    mysql_tablename($result$i)==$tablename) return true;
        }
        return 
    false;

    ma ai fini della sicurezza non è meglio usare un md5 della tabella? dato che ci sei...
    Codice PHP:
    function TableExists($tablename$db) {
        
    // Get a list of tables contained within the database.
        
    $result mysql_list_tables($db);
        
    $rcount mysql_num_rows($result);

        
    // Check each in list for a match.
        
    for ($i=0;$i<$rcount;$i++) {
            if (
    md5(mysql_tablename($result$i))==$tablename) return true;
        }
        return 
    false;

    Non sempre essere l'ultimo è un male... almeno non devi guardarti le spalle

    il mio profilo su PHPClasses e il mio blog laboweb

Permessi di invio

  • Non puoi inserire discussioni
  • Non puoi inserire repliche
  • Non puoi inserire allegati
  • Non puoi modificare i tuoi messaggi
  •  
Powered by vBulletin® Version 4.2.1
Copyright © 2025 vBulletin Solutions, Inc. All rights reserved.