dave247 Posted February 10, 2012 Share Posted February 10, 2012 Unless buffer overflows or breaking out of code to perform a new command are problems that have been solved.... I am trying to figure out the proper PHP method for setting a boundary on a variable within a script. I have this variable $name which is fed a value from $_POST['name'] from a form field. Now this form field is limited in the HTML to accept only 20 characters, but someone could easily edit the form or outgoing post data. So I want to know how to limit the variable size in the script. In other languages it could be something like this: var name(20). So how do I do that in PHP? Quote Link to comment https://forums.phpfreaks.com/topic/256835-trying-to-figure-out-how-to-limit-variable-size-to-limit-user-input/ Share on other sites More sharing options...
digibucc Posted February 10, 2012 Share Posted February 10, 2012 substr http://php.net/manual/en/function.substr.php something like: <?php $name = substr($_POST['name'], 0, 20); ?> Quote Link to comment https://forums.phpfreaks.com/topic/256835-trying-to-figure-out-how-to-limit-variable-size-to-limit-user-input/#findComment-1316688 Share on other sites More sharing options...
Pikachu2000 Posted February 10, 2012 Share Posted February 10, 2012 If validating the input, rejecting it, and redisplaying the form to the user with an error message is what you're ultimately after, strlen would be appropriate. if( strlen($variable) > 20 ) { // oh, nooooooeessss! Too long! } Quote Link to comment https://forums.phpfreaks.com/topic/256835-trying-to-figure-out-how-to-limit-variable-size-to-limit-user-input/#findComment-1316692 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.