Jump to content

Question and Answer: What is SQL Injection? How do I prevent it?


Recommended Posts

SQL Injection could represent the following:

 

n835964_37459216_6058.jpg

 

How to prevent: (this is a simple function that will always clean your strings, that I've always used)

 


function sanitize($value)
{
    //if I remember, if it was on, it automatically added slashes, so we needed to remove those slashes first
    if( get_magic_quotes_gpc() )
    {
          $value = stripslashes($value);
    }
    //newer versions of PHP have this awesome function (which handles it all for you)
    if(function_exists("mysql_real_escape_string"))
    {
          $value = mysql_real_escape_string($value);
    }
    //use addslashes for older PHP versions I believe
    else
    {
          $value = addslashes($value);
    }
    return $value;
}

 

I forgot where I get this from, but I've always stuck it in a class, called it to clean a string, and store in database.

 

Any comments, questions, suggestions? I've always been asked about how to prevent SQL injection, or what it is. So I'd figure I post something about it to help anyone who's lost on adding security to their PHP stuff :-)

@StormTheGates

 

magic_quotes is becoming depreciated, in my point of view. In fact, the reason being you could never know the format of the string you're handling, as it defaults it for you. However, the mysql_real_escape_string() is a new function being released that does the job a helluva lot better. (I suggest using the latter, older php versions will probably need to stick with addslashes//magic_quotes)

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.