0

How to validate id in php? I usually user intval($_GET['id']) however now I am dealing with large number and intval is returning them as 0.

$id = intval($_GET['id']);

this is one of my numbers 95315898521642

Note: I want to check is a number is > 0

Jaylen
  • 36,481
  • 37
  • 117
  • 208

4 Answers4

0

you may want to look at the long datatype, see how to have 64 bit integer on PHP? for more information.

Community
  • 1
  • 1
DragonZero
  • 780
  • 4
  • 8
0

You can use filter_var with FILTER_SANITIZE_NUMBER_INT

$id = filter_var($_GET['id'], FILTER_SANITIZE_NUMBER_INT);

if (!empty($id) && '-' != $id[0])
{
  echo 'Good!';
}
MichaelRushton
  • 10,628
  • 4
  • 43
  • 63
0

If you're dealing with integer numbers you can use ctype_digit()

ulentini
  • 2,392
  • 1
  • 13
  • 25
0

Max integer size in PHP is 9223372036854775807 for a 64 bit system and 2147483647 on a 32 bit system.

You can always try using is_numeric() to validate if the value is a number, or a regex such as preg_match('/^[0-9]+$/i', $_GET['id']).

Ian
  • 22,326
  • 22
  • 55
  • 96
  • I am using 32bit system so my max is 2147483647 and I want to validate if then number is > 0 – Jaylen Mar 27 '13 at 20:44