kratsg Posted October 25, 2007 Share Posted October 25, 2007 SQL Injection could represent the following: How to prevent: (this is a simple function that will always clean your strings, that I've always used) function sanitize($value) { //if I remember, if it was on, it automatically added slashes, so we needed to remove those slashes first if( get_magic_quotes_gpc() ) { $value = stripslashes($value); } //newer versions of PHP have this awesome function (which handles it all for you) if(function_exists("mysql_real_escape_string")) { $value = mysql_real_escape_string($value); } //use addslashes for older PHP versions I believe else { $value = addslashes($value); } return $value; } I forgot where I get this from, but I've always stuck it in a class, called it to clean a string, and store in database. Any comments, questions, suggestions? I've always been asked about how to prevent SQL injection, or what it is. So I'd figure I post something about it to help anyone who's lost on adding security to their PHP stuff :-) Quote Link to comment https://forums.phpfreaks.com/topic/74668-question-and-answer-what-is-sql-injection-how-do-i-prevent-it/ Share on other sites More sharing options...
StormTheGates Posted October 25, 2007 Share Posted October 25, 2007 Or just turn on magic quotes? Quote Link to comment https://forums.phpfreaks.com/topic/74668-question-and-answer-what-is-sql-injection-how-do-i-prevent-it/#findComment-377502 Share on other sites More sharing options...
kratsg Posted October 25, 2007 Author Share Posted October 25, 2007 @StormTheGates magic_quotes is becoming depreciated, in my point of view. In fact, the reason being you could never know the format of the string you're handling, as it defaults it for you. However, the mysql_real_escape_string() is a new function being released that does the job a helluva lot better. (I suggest using the latter, older php versions will probably need to stick with addslashes//magic_quotes) Quote Link to comment https://forums.phpfreaks.com/topic/74668-question-and-answer-what-is-sql-injection-how-do-i-prevent-it/#findComment-377509 Share on other sites More sharing options...
dbo Posted October 25, 2007 Share Posted October 25, 2007 Magic quotes causes more problems than it fixes and should not be trusted. Furthermore it will not be in PHP6. Quote Link to comment https://forums.phpfreaks.com/topic/74668-question-and-answer-what-is-sql-injection-how-do-i-prevent-it/#findComment-377511 Share on other sites More sharing options...
kratsg Posted October 25, 2007 Author Share Posted October 25, 2007 Magic quotes causes more problems than it fixes and should not be trusted. Furthermore it will not be in PHP6. What he said xD Quote Link to comment https://forums.phpfreaks.com/topic/74668-question-and-answer-what-is-sql-injection-how-do-i-prevent-it/#findComment-377517 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.