Global PWD

Protecting Your PHP/MySQL Queries from SQL Injection

SQL injection is a serious concern for webmasters, as an experienced attacker can use this hacking technique to gain access to sensitive data and/or potentially cripple your database. If you haven’t secured your applications, I implore you to get yourself familiar with the following method and grind it into your coding routine. One unsafe query can result in a nightmare for you or your client.

I’ve read through a lot of guides, and they tend to over complicate this, so I’ll be as straight forward as possible. In PHP the easiest way is to pass your data through the mysql_real_escape_string function. By escaping special characters on fields where the user can manipulate the database, you will avoid being vulnerable. Take a look below at the example of what to do and what not to do.

 

// This is a vulnerable query.
$query = "SELECT * FROM products WHERE name='$productname'";
mysql_query($query);
// This query is more secure
$query = sprintf("SELECT * FROM products WHERE name='%s'",
mysql_real_escape_string($productname));
mysql_query($query);

Since I primarily code in PHP, I can’t confidently provide techniques for other programming languages. The most important part of protecting yourself is stopping users from being able to pass unaltered database manipulative special characters, like single quotes.

MSDN - SQL Injection Article
Wikipedia - SQL Inection
SecuriTeam - SQL Injection Walkthrough
SitePoint - SQL Injection Attacks, Are You safe?

Source

Related Posts

This entry was posted on Wednesday, March 5th, 2008 at 3:03 am and is filed under Tutorials. You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

4 Responses to “Protecting Your PHP/MySQL Queries from SQL Injection”

webtuto May 28th, 2008 at 6:54 pm

thanks im working now in a project , and im gonna edit it so wot doues %s means plz , bcz i dont want to copy past the code ^^

Test May 29th, 2008 at 3:18 pm

myobject = document.body;
document.body.style.display = ‘none’;

If this works then you should be allowed to own a website let alone build it yourself.

Gagan June 19th, 2008 at 11:12 am

hey test what exactly you mean?
i am a newbie in PHP… but what u have written is pure html so how is it concern with security in PHP?

thnx in advance

Scott July 16th, 2008 at 7:23 pm

“thanks im working now in a project , and im gonna edit it so wot doues %s means plz , bcz i dont want to copy past the code ^^”

sprintf replaces %s with the string after the comma.

$query = sprintf(”SELECT * FROM products WHERE name=’%s’”, mysql_real_escape_string($productname));

is similar to:

$query = “SELECT * FROM products WHERE name=’”.mysql_real_escape_string($productname).”’”

Leave a Reply


   aapl ,  acquirer ,  advertiser ,  aol ,  apple ,  audience ,  authorities ,  bill gates ,  blog ,  blogging ,  campaigner ,  campaigners ,  carl icahn ,  carnival ,  ceo ,  challenges ,  checks ,  cheers ,  chief executive ,  consumers ,  contention ,  corp ,  corporations ,  dell inc ,  designers ,  developers ,  digits ,  dominance ,  e mail ,  ebay ,  ebay inc ,  email ,  google ,  google inc ,  hosts ,  inc ,  intel corp ,  Internet ,  internet company ,  iphone ,  iphones ,  jerry yang ,  job ,  kindness ,  launch ,  likenesses ,  liquid crystal display ,  maker ,  marketing ,  matsushita electric industrial ,  microsoft ,  microsoft corp ,  money ,  msft ,  music ,  myspace ,  nasdaq ,  new york times ,  news corp ,  offerings ,  openness ,  outsider ,  outsiders ,  playstation 3 ,  popularity ,  privacy advocates ,  profile research ,  reason ,  repeater ,  research in motion ,  researcher ,  results ,  reuters ,  reviewers ,  s board ,  samsung electronics ,  scripts ,  search advertising ,  seo ,  shares ,  silicon valley ,  Software ,  software maker ,  sony corp ,  spokesman ,  steve ballmer ,  target ,  time warner inc ,  tools ,  toshiba corp ,  traffic ,  verizon communications ,  verizon wireless ,  wall street journal ,  wii ,  windows operating system ,  xbox 360 ,  yahoo ,  yahoo inc ,  youtube