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
  • 39,043
  • 40
  • 128
  • 221

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
  • 810
  • 5
  • 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!';
}
Michael
  • 11,912
  • 6
  • 49
  • 64
0

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

ulentini
  • 2,413
  • 1
  • 14
  • 26
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
  • 24,116
  • 22
  • 58
  • 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