vijdev Posted June 12, 2010 Share Posted June 12, 2010 is this an overkill of functions, or is it good?..is there any redundancy, or is it falling short in anyway?? am getting the name from a form: $name=trim(addslashes(strip_tags($_POST['name']))); (once above is done, then $name is passed thru a regex test and lastly ofcourse am also doing a mysql_real_escape($name) before dumping into the DB - am sure of these two activities, but not the trim/addslash/striptag) Quote Link to comment https://forums.phpfreaks.com/topic/204535-trimaddslashstrip_tag-for-a-form-input-variable/ Share on other sites More sharing options...
jcbones Posted June 12, 2010 Share Posted June 12, 2010 If you are using mysql_real_escape_string() there is no need to addslashes. Trim takes away whitespace from the ends, and strip_tags strips out any HTML. If you are checking by regex, you could eliminate all three of those functions. $pattern = '~[^A-Za-z]~'; //Allow only alpha characters. This gets rid of whitespace, or HTML brackets, or quotes, commas, dashes, etc. $name = preg_replace($pattern,'',$_POST['name']); Lastly, I would pass it through mysql_real_escape_string(). This will add slashes for the DB, just like you stated. Quote Link to comment https://forums.phpfreaks.com/topic/204535-trimaddslashstrip_tag-for-a-form-input-variable/#findComment-1070991 Share on other sites More sharing options...
mrMarcus Posted June 12, 2010 Share Posted June 12, 2010 If you are using mysql_real_escape_string() there is no need to addslashes. Trim takes away whitespace from the ends, and strip_tags strips out any HTML. If you are checking by regex, you could eliminate all three of those functions. $pattern = '~[^A-Za-z]~'; //Allow only alpha characters. This gets rid of whitespace, or HTML brackets, or quotes, commas, dashes, etc. $name = preg_replace($pattern,'',$_POST['name']); Lastly, I would pass it through mysql_real_escape_string(). This will add slashes for the DB, just like you stated. It will add slashes to what? You have just stripped everything out other than the alphabet. mysql_real_escape_string() would be a waste of time. Quote Link to comment https://forums.phpfreaks.com/topic/204535-trimaddslashstrip_tag-for-a-form-input-variable/#findComment-1070996 Share on other sites More sharing options...
chmpdog Posted June 12, 2010 Share Posted June 12, 2010 it will add slashes to ' and " so this: ' will be: \' Quote Link to comment https://forums.phpfreaks.com/topic/204535-trimaddslashstrip_tag-for-a-form-input-variable/#findComment-1071009 Share on other sites More sharing options...
mrMarcus Posted June 12, 2010 Share Posted June 12, 2010 it will add slashes to ' and " so this: ' will be: \' I was referring to running a regex against a value such as the one previously described would prove escaping characters redundant. For example: <?php $some_var = "John's special variable"; $stripped = preg_replace('/([^a-z\s]+)/i', '', $some_var); //$stripped = Johns special variable ?> So, after using preg_replace, the need to escape any characters is not needed as the single quote was removed (replaced). Quote Link to comment https://forums.phpfreaks.com/topic/204535-trimaddslashstrip_tag-for-a-form-input-variable/#findComment-1071016 Share on other sites More sharing options...
vijdev Posted June 12, 2010 Author Share Posted June 12, 2010 thank you gentlemen!....it was helpful..! Quote Link to comment https://forums.phpfreaks.com/topic/204535-trimaddslashstrip_tag-for-a-form-input-variable/#findComment-1071079 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.