Author Topic: Protecting Your PHP/MySQL Queries from SQL Inection  (Read 1368 times)

0 Members and 1 Guest are viewing this topic.

inetbiz

  • eCommerce Strategy Consultant
  • Administrator
  • Full Member
  • *****
  • Offline Offline
  • Posts: 133
  • Karma: 22
  • SKYNET; T3; Apple Inc. Coincidence?
    • View Profile
    • Hosting for Creloaded Cart
Protecting Your PHP/MySQL Queries from SQL Inection
« Reply #1 on: November 24, 2008, 03:23:15 PM »
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.

The author read through a lot of guides, and they tend to over complicate this, so they were 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.

Code: [Select]
// 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);

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?

31 Comments
« Last Edit: November 24, 2008, 03:25:18 PM by inetbiz »