Jump to content

User input


stribor40

Recommended Posts

How to ensure that user input is integer

Beside checking is_int(var) or intval, i also want to make sure it is not empy and not null. Only input i would like that int is between 1 and 1000. What happens if user puta just this " ". Or doesnt put anything ?

Is there any way to ensure that it is only integer with php.

Link to comment
Share on other sites

What i am actually trying to do is this.....

I will pass variable to my script like this. Http://mydoman.com/script.php?user=125

 

So now i want my script to do something with 125. I want to account for in the case user tries to put something like this in url

?user=

Or ?user=hshahahah

Or ?user

Or ?

 

Basically i just want to be prepared and handle any input into url that is not integer

Link to comment
Share on other sites

 

if(!empty(trim($val))

 

That is invalid, unless you are using PHP 5.5, which is unlikely for most people as it's still pretty new. empty can only be used on variables in previous versions, not expressions.

 

$user = isset($_GET['user'])?trim($_GET['user']):'';
if ($user == '' || !ctype_digit($user) || $user > 1000 || $user < 1){
   //Error 
}
else {
   //Ok
}
That will assign $user to a trimmed version of the $_GET variable if it exists, or the empty string otherwise. The if then checks if:

- It is the empty string or

- It contains a non-digit (0-9) character or

- It is > 1000 or

- It is < 1

 

If any of those are true, you have an error condition which you'd handle in the body of the if statement.

Edited by kicken
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.