Simple clear advice in plain English

Hands on: SQL injections

Prevent SQL injections with the help of PHP

I’m quite a fan of using PHP and MySQL to create sites, and I also like the ease with which tools such as Adobe Dreamweaver can help you create pages that request information from a database.

You can build a page that lists all the parts in your database, for example, and then add a dropdown menu that filters by category, just calling the same page again with a parameter.

But this is where problems can creep in. You might have a page, which started out as something like partslist.php. You’ve decided to add a dropdown menu and refine the query in Dreamweaver, which makes it easy to check a query string and select data based on that.

So you end up with a script with a URL that looks like partslist.php?catid=brakes.
You check the PHP code and see something that looks like this:
$colname_parts = “1”;if (isset($_GET[‘catid’])) {
$colname_parts = (get_magic_quotes_gpc()) ? $_GET[‘catid’] : addslashes($_GET[‘catid’]);
}

The addslashes function stops people putting any funny business in there, doesn’t it? Well, no. It puts slashes before quotes, but that’s it. Doesn’t PHP’s MySQL function allow only one command at a time, helping prevent problems? Sort of.

There are many ways to get useful information from a script without needing to add an extra query. The UNION command in SQL is a favourite.

Dreamweaver will typically assume that the $colname_parts variable is OK and build a query incorporating it, which will look something like this:

SELECT partno, name, description, category, price FROM parts WHERE category = %s
replacing the %s with the variable. Then someone comes to your site and feeds it a query string that looks like so:
catid=-9999+union+all+select+1,concat(username,char(58),email),3,null,5,6,null+from+users

Reader Comments

   

Add your comment

All fields must be completed. Your email address will not be displayed or used to send marketing messages.

All messages will be checked by moderators before appearing on the site.

See our Privacy Policy for more information.

Related articles

Hands on: Foreign characters

Dealing with letters that have accents and diacritical marks in assorted languages

How to use Perl or PHP scripts on your web site

Automate tasks and improve ease of use when processing data on the web

Question & Answer

Q.How do I store musician and other information about...

> Read the answer

Q.Why can't my browser find the website address I typed...

> Read the answer

Q.All updates have been downloaded, so why won't Windows...

> Read the answer

Best deals on the web

img

THREE E585 Mi-Fi Take it Away Mobile Broadband - 5GB allowance

£44.97- Buy it now

img

T-MOBILE 3G Pay As You Go iPad Micro SIM

£0.10- Buy it now

img

THREE Huawei E353u Take It Away Mobile Broadband - One Month Rolling Contract

£4.99- Buy it now

Great benefits for subscribers!

Poll

Which is your preferred web browser

Jargon Buster

Computing terms explained in plain English

CAD

Computer Aided Design. Software used to create 3D models.

Great shopping deals from Computeractive