I have the following code
$page = $_GET['p'];
if($page == "")
{
$page = 1;
}
if(is_int($page) == false)
{
setcookie("error", "Invalid page.", time()+3600);
header("location:somethingwentwrong.php");
die();
}
//else continue with code
which I am going to use for looking at different "pages" of a database (results 1-10, 11-20, etc). I can't seem to get the is_int() function to work correctly, however. Putting "1" into the url (noobs.php?p=1) gives me the invalid page error, as well as something like "asdf".
Using
is_numeric()
for checking if a variable is an integer is a bad idea. This function will returnTRUE
for3.14
for example. It's not the expected behavior.To do this correctly, you can use one of these options:
Considering this variables array :
The first option (FILTER_VALIDATE_INT way) :
Output :
The second option (CASTING COMPARISON way) :
Output :
The third option (CTYPE_DIGIT way) :
Output :
The fourth option (REGEX way) :
Output :