![]() |
Easy Peasy Variable Validation
Doing validation whether it is for security purposes or just validating user input is very easy without using regex.
Lets take a look at our variables PHP Code:
Now lets validate our variables, we'll start by validating our variables with is_numeric() PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
PHP Code:
|
Quote:
PHP Code:
is_* functions mentioned above blindly on user input as they might not behave precisely how you expect. For example, from the sample above, $_GET['foo'] is not a float and $_GET['bar'] is not boolean.Also, the information provided about is_bool is incorrect. The function checks the data type of the variable so only $bool would return TRUE, the others would all return FALSE (go check for yourself).It is also worth mentioning that is_numeric checks whether the variable is a number or a numeric string. For example, the following are all numeric but you might not want to allow them: "0123", "0xFF", "+123.4567e8". |
Hehe sorry for that should have word the post a little different yes is_numeric would check for any numeric value in a given string or variable so fsda342 would be True.
Of course for user input you could always use str_replace to remove the quotes and thus u have a clean value. I didn't include this but here is an example. PHP Code:
|
I don't know if we're running different versions of PHP or something but I can't seem to correlate what you say with my results from sample code.
is_numeric('fsda342') will return false, it's quite obvious that that string isn't a representation of a number. As for the user input comments, I only wrapped the values in quotes to denote that they were indeed strings rather than their integer/float counterparts.As for is_bool, I've never had a problem with it and after a quick search I can't see any bugs filed for the function always returning true and it's certainly not expected behaviour. Does anyone else here have the same results? |
| All times are GMT. The time now is 08:03 PM. |
Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0