kaliok Posted June 12, 2008 Share Posted June 12, 2008 Hi All I just wanted to get some feedback on some code I am planning on using to stop SQL injection (if it is at all possible with the following code). The scenario would be the user would input some search criteria. The search criteria would be somewhat Googlesk in nature, for example: pet* +dog -cat -"golden retriever" So I need to allow: backslashes,stars, and plus signs so that the user can use some of the capabilities of the boolean mode search. Is the following secure enough to stop a sql injection, I have done some tests but perhaps someone could have a look and point out any flaws and fixes please. .... $thesearch=trim(stripslashes(strip_tags(mysql_real_escape_string(@$_POST['ud_mysearch'])))); $thesearch=strtr($thesearch,',/&()$%^@~`?;',''); $queryGC="SELECT *,MATCH(keywords) AGAINST ('$thesearch' IN BOOLEAN MODE) AS score FROM images WHERE MATCH(keywords) AGAINST ('$search' IN BOOLEAN MODE)"; .... Thanks. Link to comment https://forums.phpfreaks.com/topic/109899-sql-injection-prevention-for-search-boxes/ Share on other sites More sharing options...
RMcLeod Posted June 12, 2008 Share Posted June 12, 2008 When nesting PHP functions PHP performs them from the inside out, therefore stripslashes will remove any slashes added by mysql_real_escape_string. Link to comment https://forums.phpfreaks.com/topic/109899-sql-injection-prevention-for-search-boxes/#findComment-563921 Share on other sites More sharing options...
kaliok Posted June 12, 2008 Author Share Posted June 12, 2008 Ok. Thanks. So I'll remove one or tother of those. Is the code still and/or now vulnerable? .... $thesearch=trim(mysql_real_escape_string(@$_POST['ud_mysearch'])); $thesearch=strtr($thesearch,',/&()$%^@~`?;',''); $queryGC="SELECT *,MATCH(keywords) AGAINST ('$thesearch' IN BOOLEAN MODE) AS score FROM images WHERE MATCH(keywords) AGAINST ('$search' IN BOOLEAN MODE)"; .... Link to comment https://forums.phpfreaks.com/topic/109899-sql-injection-prevention-for-search-boxes/#findComment-563924 Share on other sites More sharing options...
kaliok Posted June 12, 2008 Author Share Posted June 12, 2008 Actually it appears I do need to use both stripslashes and mysql_real_escape_string to allow me to use quotes (as in the example in the original question). From what I can see on the examples given on the php.net site this shouldnt be a problem. At any rate I am still confused as to whether the code I have used is sufficient to stop an attack. Link to comment https://forums.phpfreaks.com/topic/109899-sql-injection-prevention-for-search-boxes/#findComment-563968 Share on other sites More sharing options...
RMcLeod Posted June 13, 2008 Share Posted June 13, 2008 Is magic quotes switched on? If so you don't need mysql_real_escape_string as magic quotes will automagically add slashes for you. If it's turned off then as I said stripslashes will undo the work of mysql_real_escape_string. Link to comment https://forums.phpfreaks.com/topic/109899-sql-injection-prevention-for-search-boxes/#findComment-564590 Share on other sites More sharing options...
conker87 Posted June 13, 2008 Share Posted June 13, 2008 Is magic quotes switched on? If so you don't need mysql_real_escape_string as magic quotes will automagically add slashes for you. If it's turned off then as I said stripslashes will undo the work of mysql_real_escape_string. If they're switched on, then remove the slashes they do and use mysql_real_escape_string.. MAGIC_QUOTES are awful. Link to comment https://forums.phpfreaks.com/topic/109899-sql-injection-prevention-for-search-boxes/#findComment-564592 Share on other sites More sharing options...
RMcLeod Posted June 13, 2008 Share Posted June 13, 2008 If they're switched on, then remove the slashes they do and use mysql_real_escape_string.. MAGIC_QUOTES are awful. I totally agree with you, but it's not always possible for people to turn them off, it depends on their hosting provider. Link to comment https://forums.phpfreaks.com/topic/109899-sql-injection-prevention-for-search-boxes/#findComment-564643 Share on other sites More sharing options...
conker87 Posted June 13, 2008 Share Posted June 13, 2008 There's a bit of code to remove slashes: <?php if (get_magic_quotes_gpc()) { function stripslashes_array($array) { return is_array($array) ? array_map('stripslashes_array', $array) : stripslashes($array); } $_COOKIE = stripslashes_array($_COOKIE); $_FILES = stripslashes_array($_FILES); $_GET = stripslashes_array($_GET); $_POST = stripslashes_array($_POST); $_REQUEST = stripslashes_array($_REQUEST); } ?> And you're all set to do your own stripping. If you have an include file, then stick that at the top of the page. Link to comment https://forums.phpfreaks.com/topic/109899-sql-injection-prevention-for-search-boxes/#findComment-564645 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.