Forum Moderators: coopster
This is an example of one part of the code:
$status_new = $_POST['status_new'];
mysql_query("UPDATE users SET user_status='$status_new' WHERE user_id='$user_info[user_id]' LIMIT 1");
Now is it me, or is that horrifically dodgy? Taking a user submitted variable and dumping it straight into a SQL code. Ideal for a SQL injection surely? Also the none enclosed array ref aint to hot.
Or am I barking up the wrong tree?
mysql_query("UPDATE users SET user_status='$status_new' WHERE user_id='" . $user_info['user_id'] . "' LIMIT 1");
to be honest I would change it to
$q = "UPDATE users SET user_status='$status_new' WHERE user_id='" . $user_info['user_id'] . "' LIMIT 1";
mysql_query($q);
and that user data needs some cleaning
it's great that you are actually looking through this billuk, most people just use it however they get it.
I was going to use the mysql_escape_string wrapped around the user input.
eg.
$q = "UPDATE users SET user_status='$status_new' WHERE user_id='" . mysql_escape_string($user_info['user_id']) . "' LIMIT 1";
mysql_query($q);
This does protect things to a certain extent, doesn't it?
Remember that the "real" version needs an open connection to the db to work as it takes into account the charset
it does help a little, depending on what data you expect there are a lot of other tests that could be done
The programmer may have been assuming the server had some nonstandard PHP settings like magic quotes, or he may have done cleaning on the entire $_POST array. You might make sure this is the case before you go too far--I've run into several programmers that use the (annoying, but at least secure) practice of sanitizing all $_POST, $_GET, and $_COOKIE variables at the top of their code and then just assigning all the clean values back into those arrays.
Here are some of the other key characters to test, according to php.net:
\x00, \n, \r, \, ', " and \x1a
All of those should be escaped, whether single-byte or multi-byte.
Find the hacks which worked against mysql_escape_string() (easy enough to find) and test them against this homespun escaping function.
Can you tell me why you think it is not a good practice?
Here are mine:
good practice would be to write cleaned information to a new array or set of variables and deal with that. Destroying the original data is bad form for the exact reason that billuk didn't realize it was getting cleaned. Also what happens if you forget to include your cleaning function? You would still be working with the $_POST array having no clue that you are using bad data.
I also don't agree with changing data submitted by the user, send it back to them to fix. (I'm not saying that's happening but if it is..)