+ Reply to Thread
Results 1 to 3 of 3

Thread: Against SQL injection (PHP-MySQL)

  1. #1
    hipro1 is offline x10Hosting Member hipro1 is an unknown quantity at this point
    Join Date
    Jul 2009
    Posts
    22

    Against SQL injection (PHP-MySQL)

    Well my doubt is, which are the best ways to protect my site from SQL injection ?
    How and when to protect my forms ?
    The best programming solution!

  2. #2
    descalzo's Avatar
    descalzo is offline Grim Squeaker descalzo has a brilliant futuredescalzo has a brilliant futuredescalzo has a brilliant future
    Join Date
    Jul 2009
    Location
    Ankh-Morpork
    Posts
    7,636

    Re: Against SQL injection (PHP-MySQL)

    I always use

    $escaped_string = mysqli_real_escape_string ( $link , $unsafe_input_string )
    where $link is the db connection returned by mysqli_connect and $unsafe_input_string is any info that comes from a form. Any info, even hidden fields.
    Last edited by descalzo; 08-08-2009 at 07:12 PM.
    Nothing is always absolutely so.

  3. #3
    misson is offline x10 Spammer misson is a jewel in the rough
    Join Date
    Mar 2008
    Location
    Libertatia
    Posts
    2,506

    Re: Against SQL injection (PHP-MySQL)

    The more modern approach is to use prepared statements (see also mysqli::prepare()):

    PHP Code:
    $db = new PDO("mysql:host=localhost;dbname=$dbName"$dbUser$dbPassword);

    # Named parameters
    $stmt $db->prepare("SELECT id, surname, given_name, birthday FROM users WHERE surname=:surname AND given_name=:given_name");
    $stmt->execute(array(':surname' => 'Derf'':given_name' => 'Fred'));
    while (
    $row $stmt->fetch()) {
        ....
    }

    // assumes $_POST has keys 'surname' and 'given_name'
    for ($_POST as $key => $val) {
      
    $stmt->bindValue(":$key"$val);
    }
    $stmt->execute();
    while (
    $row $stmt->fetch()) {
        ....
    }


    # Positional parameters
    $stmt $db->prepare("SELECT id, surname, given_name, birthday FROM users WHERE surname=? AND given_name=?");
    $stmt->execute(array('Derf''Fred));
    while ($row = $stmt->fetch()) {
        ....

    Of course, production code should look quite different. The DB user's credentials should be squirreled away in a function or class that creates DB connections, with the function/class itself squirreled away in a script that's only readable by the file's owner. The statement preparation, parameter binding and execution should be placed in a data access layer (DAL), so the other components of the site aren't aware of the database. Errors should be properly handled.

    get_class_vars(), get_object_vars(), PDOStatement->fetchObject() and PDOStatement->setFetchMode(), along with the array functions, can all help translate between persistent storage and PHP objects.
    Be sure to read all pages linked in this post; they have further information that should prove useful. When asking for help, make sure you follow Eric Raymond's and Jon Skeet's guidelines for prompt, accurate responses. Please answer any questions I ask; they're not rhetorical (probably). Any posted code is intended as illustrative example, rather than a solution to your problem to be copied without alteration. Study it to learn how to write your own solution.
    Misson, not Mission.

+ Reply to Thread

Similar Threads

  1. Which is better postgresql or MySQL?
    By Kurai Kumo in forum Scripts & 3rd Party Apps
    Replies: 11
    Last Post: 12-10-2009, 08:17 AM
  2. PHP MySQL Question..
    By anuj_web in forum Programming Help
    Replies: 7
    Last Post: 04-26-2008, 05:43 AM
  3. PHP and SQL issues.
    By DarkDragonLord in forum Free Hosting
    Replies: 17
    Last Post: 03-29-2007, 10:05 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
x10hosting free hosting for the masses
dedicated servers