Page 1 of 1

Validating IDs in php using intval ()?

Posted: Mon Aug 27, 2012 9:49 am
by mcc_shane
Hi Everyone,

I'm trying to figure out the correct syntax when trying to validate this line of code using intval ().

Code: Select all

$id = ereg_replace("[^0-9]", "", $_GET['id']); // filter everything but numbers for security
if ($id == "") {
	echo "Missing Data to Run";
	exit();
}
Below is what I think is the correct syntax but I''m getting an error message and I'm not sure what I'm doing wrong.
http://whatsmyowncarworth.com/more-prac ... .php?id=10
Error message "Missing Data to Run"

(I think this is the correct way but my syntax is wrong. What's the correct syntax?)

Code: Select all

$id = $_GET['id'] = 1;
if (filter_var($id, FILTER_VALIDATE_INT) !== false)
{
  echo "Missing Data to Run";
  exit();
}
Any help would be appreciated! Thanks everyone!

Re: Validating IDs in php using intval ()?

Posted: Mon Aug 27, 2012 10:03 am
by Benjamin

Code: Select all

if (!preg_match('#^\d+$#', $id)) {
    #error
}

Re: Validating IDs in php using intval ()?

Posted: Mon Aug 27, 2012 12:46 pm
by requinix