I have posted a few threads on this forum relating to a huge loss of data at my website. It was a code flaw, and it was my fault, but a good wakeup call nonetheless.
I have been looking through/posting my code to get tips and opinions on it. One thing that everybody has mentioned I need to look at is preventing SQL Injections.
When somebody fills out a form on my site, for example, depending on what they want to do (i.e: remove post, add post, remove all posts) I assign a code (i.e: code 1, code 2, code 3) and I propagate this in the URL as c=1, 2, 3. Then I handle it using the $_GET command when coding the function
I have been made aware that doing this would make my site EXTREMELY vulnerable to SQL injections. Now my question is this:
$code=mysql_real_escape_string($_GET['code']);
$_SESSION['username']=mysql_real_escape_string($_SESSION['username']);
$post_id=mysql_real_escape_string($post_id);
//etc.
//etc.
//////////////////////////////check if post_id/code is in range for preventing sql injection
if(!($post_id==1 || $post_id==2 || $post_id==3 || $post_id==4 || $post_id==5 || $code==1 || $code==2))
{
die('Invalid file ID/code. Please contact us if this problem persists.');
}
//////////////////////////////
//etc.
//etc.
//for example:
$query = "UPDATE posts SET post".$post_id."_name = '$post_name' WHERE username = '".$_SESSION['username']."'";
Would this thwart any sql injection possibilities?
I have gone through all my code and ANYTHING that passes through an SQL command/query I have run through the mysql_real_escape_string() command. Could this hurt me at all? Is this a good or bad idea?
Thanks.