Validating IDs in php using intval ()?

Discussion in 'PHP' started by scm22ri, Aug 27, 2012.

  1. #1
    Hi Everyone,


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


    $id = ereg_replace("[^0-9]", "", $_GET['id']); // filter everything but numbers for securityif ($id == "") {    echo "Missing Data to Run";    exit();} 
    PHP:

    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-practice/member_profile.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?)


    $id = $_GET['id'] = 1;if (filter_var($id, FILTER_VALIDATE_INT) !== false){  echo "Missing Data to Run";  exit();}
    PHP:

    Any help would be appreciated! Thanks everyone!
     
    scm22ri, Aug 27, 2012 IP
  2. afstanislav

    afstanislav Greenhorn

    Messages:
    31
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    16
    #2
    try to use ctype_digit - Check for numeric character(s).

    Returns TRUE if every character in the string text is a decimal digit, FALSE otherwise.
     
    afstanislav, Aug 27, 2012 IP
  3. fastestsms

    fastestsms Greenhorn

    Messages:
    72
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    6
    #3
    I think that PHP have an is_numeric function.

    Returns TRUE if var is a number or a numeric string, FALSE otherwise.
     
    fastestsms, Aug 27, 2012 IP