What happened to the Messenger Plus! forums on msghelp.net?
Shoutbox » MsgHelp Archive » Skype & Technology » Tech Talk » Who knows something about SQL and PHP

Who knows something about SQL and PHP
Author: Message:
surfichris
Former Admin
*****

Avatar

Posts: 2365
Reputation: 81
Joined: Mar 2002
RE: Who knows something about SQL and PHP
It would go on the lines right before mysql_query("INSERT INTO...");

Yes, you're just a regular guy, but it is those regular guys whose websites get hacked because they don't know things like this.

Essentially any information you save to a database from user input needs to be sanitized to prevent special characters performing unwanted things (SQL injection etc)

So essentially any incoming data you run mysql_real_escape_string on before you insert or run a query using it. If you're inserting an integer from user input, typecast it to an integer first.

For example:
String: My test' string

Result unescaped: INSERT INTO test ('abc') VALUES ('My test' string');
After mysql real escape string: INSERT INTO test ('abc') VALUES('My test\' string');

Notice how in the unescaped version there is an extra quote in there? We don't want that, it is bad and cause malicious things.

Second example of typecasting:

Incoming Integer (number): abc

Notice how it isn't a number?

Query: "SELECT * FROM test WHERE test=".$_POST['integer'].";";
Resulting Query: SELECT * FROM test WHERE test=abc.

Now we have a problem. Because we want to be querying using an integer and a malicious user has entered a text string and we aren't quoting and escaping the value (you don't have to for integers) then whatever they enter can be executed as an additional query.

Solution?

Query: "SELECT * FROM test WHERE test=".(int)$_POST['integer'].";";
Resulting Query: SELECT * FROM test WHERE test=0

Because we've casted the data to an integer and abc is not an integer (and doesn't contain any), 0 is returned, thus in this example we're protected.

This is only a subset of what you need to look out for but it covers the basics.

Chris
01-06-2008 11:35 AM
Profile PM Find Quote Report
« Next Oldest Return to Top Next Newest »

Messages In This Thread
Who knows something about SQL and PHP - by Exca on 01-05-2008 at 04:31 PM
RE: Who knows something about SQL and PHP - by NanaFreak on 01-05-2008 at 04:40 PM
RE: Who knows something about SQL and PHP - by Exca on 01-05-2008 at 04:42 PM
RE: Who knows something about SQL and PHP - by NanaFreak on 01-05-2008 at 04:43 PM
RE: Who knows something about SQL and PHP - by Exca on 01-05-2008 at 04:45 PM
RE: Who knows something about SQL and PHP - by NanaFreak on 01-05-2008 at 04:48 PM
RE: Who knows something about SQL and PHP - by Exca on 01-05-2008 at 04:53 PM
RE: Who knows something about SQL and PHP - by Exca on 01-05-2008 at 04:57 PM
RE: Who knows something about SQL and PHP - by NanaFreak on 01-05-2008 at 05:00 PM
RE: Who knows something about SQL and PHP - by Exca on 01-05-2008 at 05:04 PM
RE: Who knows something about SQL and PHP - by surfichris on 01-05-2008 at 10:07 PM
RE: Who knows something about SQL and PHP - by Exca on 01-05-2008 at 10:33 PM
RE: Who knows something about SQL and PHP - by Tochjo on 01-05-2008 at 10:35 PM
RE: Who knows something about SQL and PHP - by Exca on 01-05-2008 at 10:42 PM
RE: Who knows something about SQL and PHP - by surfichris on 01-06-2008 at 11:35 AM
RE: Who knows something about SQL and PHP - by Exca on 01-06-2008 at 12:43 PM
RE: Who knows something about SQL and PHP - by Volv on 01-09-2008 at 11:36 AM


Threaded Mode | Linear Mode
View a Printable Version
Send this Thread to a Friend
Subscribe | Add to Favorites
Rate This Thread:

Forum Jump:

Forum Rules:
You cannot post new threads
You cannot post replies
You cannot post attachments
You can edit your posts
HTML is Off
myCode is On
Smilies are On
[img] Code is On