SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    Now available in Orange Tijmen's Avatar
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    1,469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    $_GET check if exists and not a letter

    Hi,

    I'm trying to check if the user_id is set, and if it's a number instead of a letter. If any of these 2 fail it should redirect the page back to the index.

    This works for the first part, if the user_id is not in the url you are going back to the index. But when i enter a letter instead of a number it only comes up with an error. I try'd the is_numeric check seperately and this works fine, but i can't get it to work together.

    Any suggestions how to fix it, or a better way to make sure the user_id is a number and does exist in the url.

    Code:
     if(!isset($_GET['user_id']) && (!is_numeric($_GET['user_id']))) { 
       header ("Location: http://" . $_SERVER['HTTP_HOST'] . dirname($_SERVER['PHP_SELF']) . "/index.php");
     }

  2. #2
    Tranceoholic lilleman's Avatar
    Join Date
    Feb 2004
    Location
    Írebro, Sweden
    Posts
    2,716
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You have to use or instead of and, since you want the IF-statement to be true when one condition fail:

    PHP Code:
    if(!isset($_GET['user_id']) or !is_numeric($_GET['user_id'])) { 
    ERIK RIKLUND :: Yes, I've been gone quite a while.

  3. #3
    Now available in Orange Tijmen's Avatar
    Join Date
    Jul 2004
    Location
    The Netherlands
    Posts
    1,469
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks, works beter now.

  4. #4
    SitePoint Addict
    Join Date
    Oct 2004
    Location
    Brooklyn, NY
    Posts
    359
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    You might also want to use ctype_digit() instead of is_numeric(). They're similar, but ctype_digit() returns TRUE when all characters are a digit. This is different than is_numeric(), which returns TRUE for any valid representation of a number.
    Chris Shiflett
    http://shiflett.org/


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •