merylvingien Posted November 13, 2009 Share Posted November 13, 2009 Hi fellas, i'm back with yet another silly problem. Whilst doing some checks i have noticed a security hole in my code that i am trying to patch up. If i manipulate a sign up page, i can overwrite the database where it shouldnt be able to do so, and if i can do it, loads of other people can LOL So i put in place a check that stop this, but its not quite right. The output is messy. Here is the code block i have: if(isset($_POST['signupID'])) { foreach($_POST['signupID'] as $item) { $sql = "SELECT pagestate FROM postcode WHERE postcodeID=$item"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); $result = mysql_query($sql); $row2 = mysql_fetch_assoc($result); if($row2['pagestate'] === 'T') {echo "<p>You have tried to cheat!</p>";} else {everything is ok insert into database } } echo somestuff mail some stuff } If i try and hack my site, that check prevents it from happening, but it outputs "You have tried to cheat!" as many times as there is a $item posted. I understand that its becuase it is in the code block foreach! But i am scratching my ass trying to work out how to do it so it checks all $item and only posts the message once without echoing "somestuff" Any help!!! Its something simple i know and i make myself look very stupid everytime i post something on here... Quote Link to comment Share on other sites More sharing options...
FaT3oYCG Posted November 13, 2009 Share Posted November 13, 2009 MySQL escape function ? Quote Link to comment Share on other sites More sharing options...
pastcow Posted November 13, 2009 Share Posted November 13, 2009 foreach($_POST['signupID'] as $item) { $item = mysql_escape_string($item) etc... Quote Link to comment Share on other sites More sharing options...
alpine Posted November 13, 2009 Share Posted November 13, 2009 This is perhaps what you are asking for when using your current code if(isset($_POST['signupID'])) { $cheater = false; foreach($_POST['signupID'] as $item) { $sql = "SELECT pagestate FROM postcode WHERE postcodeID=$item"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); $result = mysql_query($sql); $row2 = mysql_fetch_assoc($result); if($row2['pagestate'] === 'T') {$cheater = true;} else {everything is ok insert into database } } if($cheater == true){ echo "You have tried to cheat!"; } echo somestuff mail some stuff } Quote Link to comment Share on other sites More sharing options...
merylvingien Posted November 13, 2009 Author Share Posted November 13, 2009 Thanks fellas, i tried the $item = mysql_escape_string($item) and that didnt work and i tried the last one and that outputs "You have tried to cheat!" Again and again like before, at least i dont have the other output showing now LOL I also realised that i didnt need to have the if(isset($_POST['signupID'])) { in there as it will always be set by the time this page is reached. So now i have $cheater = false; foreach($_POST['signupID'] as $item) { $sql = "SELECT pagestate FROM postcode WHERE postcodeID=$item"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); $result = mysql_query($sql); $row2 = mysql_fetch_assoc($result); if($row2['pagestate'] === 'T') {$cheater = true;} else { $sql = "insert into database"; mysql_query($sql) or trigger_error("SQL: $sql, ERROR: " . mysql_error(), E_USER_ERROR); $result = mysql_query($sql); } if($cheater == true){ echo "You have tried to cheat!"; } else if ($cheater == false) {echo"<h2>Final stage complete!</h2><p>Thank you for joining</p><br> <p>A comfirmation email has been dispatched to your inbox.</p> <p>If you do not recieve this email, please check your spam filter and allow emails to be recieved from</p>"; } mail(); mail(); } Quote Link to comment Share on other sites More sharing options...
alpine Posted November 13, 2009 Share Posted November 13, 2009 sorry - just put the final message below (outside) the foreach loop, its inside now. Quote Link to comment Share on other sites More sharing options...
merylvingien Posted November 13, 2009 Author Share Posted November 13, 2009 Ch - ching Thanking you very kindly sir! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.