Jump to content

[SOLVED] Database Insert Security


liebs19

Recommended Posts

Hi guys,

 

I have a general question about security when inserting into a database. I just now realized that magic quotes in turned on. I discovered it while testing the formatting of my mail function. One of the words I typed had a single quote in it that got escaped. My next step is to insert the data into a MySQL database. I don't have the option to turn off magic quotes because I don't have access to the php.ini. Does this mean that every time I use data sent by a POST that I need to stripslashes?

 

Are the magic quotes enough security when inserting into a database? Should I be doing more to make sure that I am not the victim of a SQL injection? Should I also use htmlentities? Are the other measures I should be taking? At what point does this become overkill?

 

Thanks,

liebs19

Link to comment
Share on other sites

I use something like:

    function clean_input($input)
    {
        if (get_magic_quotes_gpc())
        {
            $input = stripslashes($input);
        }
        
        $input = mysql_real_escape_string($input); //Escapes special characters in a string for use in a SQL statement; Rember to always use single quotes around your variable in your SQL statement. Requires a connection.
        $input = strip_tags($input); //Strip HTML and PHP tags from a string; allowable_tags can be defined, example: strip_tags($input, '<p><a>')
        $input = htmlentities($input, ENT_NOQUOTES); //Convert all applicable characters to HTML entities. quote_style: ENT_NOQUOTES; Will leave both double and single quotes unconverted. Use html_entity_decode to: Convert all HTML entities to their applicable characters
        
        return $input;
    }

    $to_insert = clean_input($_POST['to_insert']);

Link to comment
Share on other sites

Here is my version, which is recursive for arrays and has a few other options:

 

<?php
  // my_clean
  // $data - data to clean before inserting into db
  // $add_sq - true to add single quotes, false to just clean
  // $force_string - true to treat the data as a string
  // $skip_strip - true to skip the strip slashes segment
  // RETURN: properly escaped and db safe data
  function my_clean($data, $add_sq = true, $force_string = false,
                                                           $skip_strip = false){
    if(is_array($data)){
      foreach($data as $key => $val){
        $data[$key] = $this->my_clean($val, $add_sq, $force_string);
      }
    }else{
      $data = trim($data);
      if(!is_numeric($data) || $force_string){
        if(get_magic_quotes_gpc() && !$skip_strip){
          $data = stripslashes($data);
        }
        if($add_sq){
          $data = "'" . mysql_real_escape_string($data) . "'";
        }else{
          $data = mysql_real_escape_string($data);
        }
      }
    }
    return $data;
  }
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.