Jump to content

Trying to figure out how to limit variable size to limit user input


dave247

Recommended Posts

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?

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!
}

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.