PHP, MYSQL, CSS, LINUX TUTORIALS

Security Tutorial #2: Are you protected against sql injections?

There are alot of websites that are vulnerable to sql injections.
In this tutorial i’ll give you some examples with methods to protect you website.

 

Make your id from url (int)

Let’s assume that you get the id like this:

   $id = $_GET['id'];

Well with this, your site may be vulnerable.

But if you use like this it will never be vulerable from this variable:

   $id = (int)$_GET['id'];

Basically this will make your id an id, i mean the value returned is always numeric.

 

Use the mysql_real_escape_string function
This function may help you alot!

$city = $_GET['city'];
$sql = mysql_query('SELECT firstname, lastname FROM people WHERE city="'.mysql_real_escape_string($city).'" ');

Note: if magic_quotes_gpc is enabled you should use:

$city = stripslashes($_GET['city']);
$sql = mysql_query('SELECT firstname, lastname FROM people WHERE city="'.mysql_real_escape_string($city).'" ');

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>