knobby2k Posted May 18, 2011 Share Posted May 18, 2011 Hi Guys, This should be a simple one. I am looking for the best way to ensure that any illegal characters are removed from an input. I have a user registration form and login page. What I want to do is prevent against sql injection and therefore should be limiting the characters that can be entered. For example I want a username to include only letters and numbers so I ideally want to strip out any white space, illegal characters such as " ' ( ) etc... My issue with this is with the password, I would ideally like the user to be able to put as complex a password as they would like, so how would i prevent against illegal characters in this instance?? Cheers Quote Link to comment https://forums.phpfreaks.com/topic/236773-checking-for-illegal-characters-in-an-input/ Share on other sites More sharing options...
wildteen88 Posted May 18, 2011 Share Posted May 18, 2011 For example I want a username to include only letters and numbers so I ideally want to strip out any white space, illegal characters such as " ' ( ) etc... Using mysql_real_escape_string will help to prevent sql injection attacks. My issue with this is with the password, I would ideally like the user to be able to put as complex a password as they would like, so how would i prevent against illegal characters in this instance?? Just encrypt the password straight away using md5 or sha1 encryption. These only return hashes that contain letters and numbers regardless of what characters the user has in their passwords. Example echo sha1('mybad"password"'); echo "<br />"; echo md5('mybad"password"') Will return the following hashes 107577e77c5ef454152af92f84ab36f5a9fdae75 <- sha1 hash 0c2f336b9977f0c40f1782e43f57e2e2 <- md5 hash Quote Link to comment https://forums.phpfreaks.com/topic/236773-checking-for-illegal-characters-in-an-input/#findComment-1217129 Share on other sites More sharing options...
knobby2k Posted May 18, 2011 Author Share Posted May 18, 2011 For example I want a username to include only letters and numbers so I ideally want to strip out any white space, illegal characters such as " ' ( ) etc... Using mysql_real_escape_string will help to prevent sql injection attacks. My issue with this is with the password, I would ideally like the user to be able to put as complex a password as they would like, so how would i prevent against illegal characters in this instance?? Just encrypt the password straight away using md5 or sha1 encryption. These only return hashes that contain letters and numbers regardless of what characters the user has in their passwords. Example echo sha1('mybad"password"'); echo "<br />"; echo md5('mybad"password"') Will return the following hashes 107577e77c5ef454152af92f84ab36f5a9fdae75 <- sha1 hash 0c2f336b9977f0c40f1782e43f57e2e2 <- md5 hash ahhh so that would protect against someone entering... ' ' OR 1'' ...into the password field because all i'm checking for is the hash?? I'm i understanding that correctly?? Quote Link to comment https://forums.phpfreaks.com/topic/236773-checking-for-illegal-characters-in-an-input/#findComment-1217136 Share on other sites More sharing options...
wildteen88 Posted May 18, 2011 Share Posted May 18, 2011 Yes, passwords stored in your database should be hashed. Therefore when you compare the passwords within your query you need to encrypt it before hand. Quote Link to comment https://forums.phpfreaks.com/topic/236773-checking-for-illegal-characters-in-an-input/#findComment-1217140 Share on other sites More sharing options...
knobby2k Posted May 18, 2011 Author Share Posted May 18, 2011 Excellent!! last but not least, is there a quick method to strip any whitespace from an input? Cheers for your help! Quote Link to comment https://forums.phpfreaks.com/topic/236773-checking-for-illegal-characters-in-an-input/#findComment-1217142 Share on other sites More sharing options...
wildteen88 Posted May 18, 2011 Share Posted May 18, 2011 You can use trim which will remove any white space before/after a string. Or iif you don't want any spaces within a username you can use preg_replace $username = 'Bad Username'; echo preg_replace('~\s+~', '', $username); Quote Link to comment https://forums.phpfreaks.com/topic/236773-checking-for-illegal-characters-in-an-input/#findComment-1217151 Share on other sites More sharing options...
knobby2k Posted May 18, 2011 Author Share Posted May 18, 2011 You are a star mate, much appreciated!! Cheers Quote Link to comment https://forums.phpfreaks.com/topic/236773-checking-for-illegal-characters-in-an-input/#findComment-1217153 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.