February 8, 201313 yr Hii everyone, How to block sql injections and to preserve the code?? use mysqli_real_escape_string
February 9, 201313 yr use mysqli_real_escape_string It's not as simple as just using this function. You're going to have to do some real research. An example where mysqli_real_escape_string wont work: $input = mysqli_real_escape_string($input); SELECT * FROM table WHERE user_id=$input Now, imagine $input is equal to "or user_id=1 and user_id !=" You're best off using prepared statements - they're designed for this very purpose, and do have some advantages (some disadvantages too, though) Read more: http://php.net/manual/en/pdo.prepared-statements.php Basically, prepared statements send the server a "framework" of your query. If you don't want to use prepared statements, you can just cast the input data to an integer, this way none of the above is possible. mysql_real_escape_string works on strings only (hint is in the name) Good luck! Edited February 9, 201313 yr by KieranA
February 10, 201313 yr It's not as simple as just using this function. You're going to have to do some real research. An example where mysqli_real_escape_string wont work: $input = mysqli_real_escape_string($input); SELECT * FROM table WHERE user_id=$input Now, imagine $input is equal to "or user_id=1 and user_id !=" You're best off using prepared statements - they're designed for this very purpose, and do have some advantages (some disadvantages too, though) Read more: http://php.net/manual/en/pdo.prepared-statements.php Basically, prepared statements send the server a "framework" of your query. If you don't want to use prepared statements, you can just cast the input data to an integer, this way none of the above is possible. mysql_real_escape_string works on strings only (hint is in the name) Good luck! yep this is why i suggest writing queries like this SELECT * FROM table WHERE user_id='$input' with single quotes being put around the variable. I also suggest where people are taking integers such as a user id in input or the url is to filter everything else out but numbers yes i agree PDO is the best way to go but if you filter your data properly in the first place both ways can help prevent injection Edited February 10, 201313 yr by webdesigner93
February 10, 201313 yr yep this is why i suggest writing queries like this SELECT * FROM table WHERE user_id='$input' with single quotes being put around the variable. I also suggest where people are taking integers such as a user id in input or the url is to filter everything else out but numbers yes i agree PDO is the best way to go but if you filter your data properly in the first place both ways can help prevent injection Why would you put quotes around an integer? Prepared statements exist for a set of reasons. PDO != Prepared statements. They're totally different.
Create an account or sign in to comment