DamienRoche Posted September 20, 2008 Share Posted September 20, 2008 Ok, so everybody knows you are meant to use addslashes on input before adding data to a database. But what if you use a preg_match and regexp to halt the script if any input contains special characters. I currently use the second method I mentioned above and that works fine, but is there any way to bypass this? Bottom line..am I opening myself up for sql injection etc. or would you consider this secure enough? Damien. Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/ Share on other sites More sharing options...
papaface Posted September 20, 2008 Share Posted September 20, 2008 I don't really see the point in using regex to get rid of specific characters unless there is a reason you need to do so other than protecting against SQL injections. I'd stick with simply adding slashes tbh, doing both can't hurt though. Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646657 Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 mysql_real_escape_string Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646659 Share on other sites More sharing options...
redarrow Posted September 20, 2008 Share Posted September 20, 2008 all u need below post set it example <?php $name="redarrow"; $name=mysql_real_escape_string($_POST['name']); ?> Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646660 Share on other sites More sharing options...
DamienRoche Posted September 20, 2008 Author Share Posted September 20, 2008 I've used mysql_real_escape_string as well. But what do you think about using regexp to just stop the script in it's tracks? I mean, I don't filter the input because some fields shouldn't have special characters. I just use an if preg_match and regexp to detect the character and stop the script. I guess really what I'm asking is: can the regexp solution be bypassed? ..can a special character get passed a preg_match? it doesn't seem possible..but I am a noob ??? I should have also noted, this was more a discussion than a problem >> solution thing. Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646661 Share on other sites More sharing options...
redarrow Posted September 20, 2008 Share Posted September 20, 2008 u can do it the flash way lol <?php $name="redarrow"; IF (PREG_MATCH("/[a-z]/",mysql_real_escape_string($_POST['name']))){ echo $name; } ?> Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646664 Share on other sites More sharing options...
rarebit Posted September 20, 2008 Share Posted September 20, 2008 There's many instances where I don't mind users adding special characters, basically because they might need or want to, for instance if there blogging and want to use any kind of speech marks. Yes it's possible to formulate a regex statement to catch any defined characters, banned words, emails etc. And no there shouldn't be a way around it unless they get at the code. Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646666 Share on other sites More sharing options...
discomatt Posted September 20, 2008 Share Posted September 20, 2008 It's simple - the stricter you are the more secure your script will be. Using RexEx to filter input is much better than mysql_real_escape_string(); Something like this if( preg_match('%[^A-z \d]%', $_POST['var']') ) # DIE # Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646676 Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 Using RexEx to filter input is much better than mysql_real_escape_string(); Care to elaborate as to how using regex is better? edit: For SQL injection attacks, that is. Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646678 Share on other sites More sharing options...
discomatt Posted September 20, 2008 Share Posted September 20, 2008 Care to elaborate as to how using regex is better? edit: For SQL injection attacks, that is. Simple, the stricter you are with data, the less chance that an unknown loophole with mysql_real_escape_string() will affect your script. At most points people will apply multiple filters to get user data from a form on to a page... real_escape.. htmlentities/specialchars are extremely common. This can be done with a single regex prior to insertion, and will save from potential holes in 2 functions. I'm not saying there's anything wrong with mysql_real_escape_string()... I'm just saying regex is more secure, and situation depending, better. Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646692 Share on other sites More sharing options...
rarebit Posted September 20, 2008 Share Posted September 20, 2008 What doesn't 'mysql_real_escape_string()' catch (e.g. 2 functions)? And my age old (never satisfyingly answered question), surely 'addslashes()' is faster... Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646698 Share on other sites More sharing options...
.josh Posted September 20, 2008 Share Posted September 20, 2008 Upon doing some research, I concede to regex being better. SQL Injection attacks do not necessarily require the attacker to include quotes. Here's an article I found: http://www.webappsec.org/projects/articles/091007.txt Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646704 Share on other sites More sharing options...
discomatt Posted September 20, 2008 Share Posted September 20, 2008 What doesn't 'mysql_real_escape_string()' catch (e.g. 2 functions)? And my age old (never satisfyingly answered question), surely 'addslashes()' is faster... Code what you want... I'm just saying that using strict regex rules is more secure. Speed isn't always the 'best' solution. Quote Link to comment https://forums.phpfreaks.com/topic/125118-security-question/#findComment-646705 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.