Jump to content

what steps do I need to take to prevent in sql attack when I am using input in q


Recommended Posts

what steps do I need to take to prevent in sql attack when I am using input from the user in a query?

 

lets say we have the variable

 

$name = $_post['name'];
$zipcode = $_post['zipcode'];
$message = $_post['message']; // textarea

 

what should i do to these variables before I use them in either select or insert to prevent a sql attack.

 

 

 

mysql_real_escape_string escapes all the characters that could be used in a SQL injection.

 

For example:

 

<?php
mysql_query("SELECT * FROM db WHERE name = $somevar");
?>

 

could be hacked by inputting:

 

a';DROP TABLE db

 

which appears as:

 

<?php
mysql_query("SELECT * FROM db WHERE name = 'a';DROP TABLE db");
?>

 

So mysql_real_escape_string prevents that by escaping the apostrophe, quotation mark, and semicolons, thus preventing an injection.

 

You want to sanitize every input to a database, but you need to be SURE to escape long inputs, like messages, news, biographies, etc. If you don't do that, users won't be able to use any of the characters mentioned above, and you'll be extra vulnerable to attack. :P

Try reading the manual page:

 

This function is identical to mysql_real_escape_string() except that mysql_real_escape_string() takes a connection handler and escapes the string according to the current character set. mysql_escape_string() does not take a connection argument and does not respect the current charset setting.

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.