Validating IDs in php using intval ()?

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mcc_shane
Forum Newbie
Posts: 22
Joined: Sat May 12, 2012 1:47 pm

Validating IDs in php using intval ()?

Post 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!
User avatar
Benjamin
Site Administrator
Posts: 6935
Joined: Sun May 19, 2002 10:24 pm

Re: Validating IDs in php using intval ()?

Post by Benjamin »

Code: Select all

if (!preg_match('#^\d+$#', $id)) {
    #error
}
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Validating IDs in php using intval ()?

Post by requinix »

Post Reply