20

I`m wonder why this not working

    echo gettype($_GET['id']); //returns string
  if(is_int($_GET['id']))
  {
   echo 'Integer';
  }

How to validate data passing from GET/POST if it is integer ?

omtr
  • 869
  • 2
  • 7
  • 7

7 Answers7

33

Can use

$validatedValue = filter_input(INPUT_GET, 'id', FILTER_VALIDATE_INT);

See http://php.net/filter_input and related functions.

Gordon
  • 312,688
  • 75
  • 539
  • 559
28

The manual says:

To test if a variable is a number or a numeric string (such as form input, which is always a string), you must use is_numeric().

Alternative you can use the regex based test as:

if(preg_match('/^\d+$/',$_GET['id'])) {
  // valid input.
} else {
  // invalid input.
}
codaddict
  • 445,704
  • 82
  • 492
  • 529
3

To validate form data (string) as an integer, you should use ctype_digit() It returns TRUE if every character in the string text is a decimal digit, FALSE otherwise. (PHP 4 >= 4.0.4, PHP 5)

Reference: http://php.net/manual/en/function.ctype-digit.php

Matej
  • 9,548
  • 8
  • 49
  • 66
fleveillee
  • 55
  • 1
  • 6
  • `ctype_digit()` will only check a string, so `ctype_digit(1) === false`, while `ctype_digit('1') === true`. This is very useful for validating user input, as $_GET and $_POST contain strings. – kfriend Apr 28 '15 at 20:11
3

What about intval?

$int = intval($_GET['id']);
TheHippo
  • 61,720
  • 15
  • 75
  • 100
  • Great solution, but inval(0) returns 0, then in your test 0 will not be considireted as a INTEGER $value =0; if(!intval($value)) { throw new Exception(...); // Throw a exception for zero } – Mandien Mar 04 '22 at 12:10
1

Try:

if(isNumeric($_GET['id'])) {
    $cast_int = (int)$_GET['id'];
}

if(isset($cast_int)) {
    echo gettype($cast_int)."<br />\n";
    if(is_int($cast_int))
    {
        echo 'Integer'."<br />\n";
    }
} else {
    echo gettype($_GET['id'])." was passed<br />\n";
}

function isNumeric($numeric) {
    return preg_match("/^[0-9]+$/", $numeric);
}
Phill Pafford
  • 83,471
  • 91
  • 263
  • 383
0

It sounds like you are checking if a string contains an integer, rather than if that variable is an integer. If so, you should check out php's regex (regular expression) functionality. It allows you to check for very specific patterns in a string to validate it for whatever criteria. (such as if it contains only number characters)

Here's the php page http://php.net/manual/en/function.preg-match.php

and here's a cheat sheet on regular expressions (to make the $pattern string) http://regexpr.com/cheatsheet/

DeviousBlue
  • 241
  • 4
  • 15
-1

I take a slightly more paranoid approach to sanitizing GET input

function sanitize_int($integer, $min='', $max='')
{
  $int = intval($integer);
  if((($min != '') && ($int < $min)) || (($max != '') && ($int > $max)))
    return FALSE;
  return $int;
}

To be even safer, you can extract only numbers first and then run the function above

function sanitize_paranoid_string($string, $min='', $max='')
{
  $string = preg_replace("/[^a-zA-Z0-9]/", "", $string);
  $len = strlen($string);
  if((($min != '') && ($len < $min)) || (($max != '') && ($len > $max)))
    return FALSE;
  return $string;
}

Code from: http://libox.net

arbithero
  • 426
  • 3
  • 13