Is it possible to check..

Discussion in 'PHP' started by Crayz, Feb 10, 2007.

  1. #1
    Say I had the url:

    http://somedomain.com/file.php?id=x (x as a variable)

    And I also had

    http://somedomain.com/file.php

    The same, but without the ?id=x

    So what I need to do is, have a code that checks if the ?id=x exists, if it does, execute the given code, if it doesn't ignore.

    So I pretty much need to know what to use inside the If statement to check if ?id= exists in the url.

    Thanks!
     
    Crayz, Feb 10, 2007 IP
  2. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #2
    
    $url = "http://google.com/exmaple.php?idx=212";
    $data = parse_url($url);
    if($data['query'])
    {
      // do stuff....
    }
    
    PHP:
     
    krakjoe, Feb 10, 2007 IP
  3. rossdalangin

    rossdalangin Well-Known Member

    Messages:
    458
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #3
    use:

    if (isset($id)) {
    echo "This variable is set!";
    }

    or

    if (isset($_GET['id'])) {
    echo "This variable is set!";
    }

    You can download my sample code here http://dalangin.com/category/programming/
    It's a basic programming tutorial using PHP/MySQL. Hope it make useful to you. Vote me if you like it!
     
    rossdalangin, Feb 10, 2007 IP
  4. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #4
    Omfg What Was I Thinking .....
     
    krakjoe, Feb 10, 2007 IP
  5. smatts9

    smatts9 Active Member

    Messages:
    1,089
    Likes Received:
    71
    Best Answers:
    0
    Trophy Points:
    88
    #5
    I would suggest using ross' code:

    if (isset($id)) {
    echo "This variable is set!";
    }
     
    smatts9, Feb 10, 2007 IP
  6. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #6
    smatts9:

    Why in the world would you do that? $id isn't defined, the sky would fall on your head.
     
    -NB-, Feb 10, 2007 IP
  7. designcode

    designcode Well-Known Member

    Messages:
    738
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    118
    #7
    If register_globals are off then smatts9 code will not work,

    Have a look at the following code.

    $id = $_GET['id'];
    if(isset($id) && $id != "")
    {
         // Do your stuff here
    }
    PHP:
     
    designcode, Feb 11, 2007 IP
  8. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #8
    actually boys I'm not so sure we're working with the current url here as op pm'd saying the code I posted worked for him .....
     
    krakjoe, Feb 11, 2007 IP
  9. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #9
    krakjoe:

    Of course your code worked, you're checking to see if ?id= is defined on GOOGLE'S WEBSITE.
     
    -NB-, Feb 11, 2007 IP
  10. bobby9101

    bobby9101 Peon

    Messages:
    3,292
    Likes Received:
    134
    Best Answers:
    0
    Trophy Points:
    0
    #10
    if (isset($_GET['id'])) {
    echo "This variable is set!";
    }

    is better, because not all hosts have register globals on (for good reason)
     
    bobby9101, Feb 11, 2007 IP
  11. krakjoe

    krakjoe Well-Known Member

    Messages:
    1,795
    Likes Received:
    141
    Best Answers:
    0
    Trophy Points:
    135
    #11
    No, I'm not, I'm parsing a url for a query structure, not specifically one website, $url is an example, the user will already have $url set in their script by something else.
     
    krakjoe, Feb 12, 2007 IP
  12. -NB-

    -NB- Peon

    Messages:
    153
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    0
    #12
    Why parse the URL? It makes no sense in that instance. It's much easier to define the query strings with the $_GET variable or the $_SERVER array rather than parsing the URL.
     
    -NB-, Feb 15, 2007 IP
  13. rossdalangin

    rossdalangin Well-Known Member

    Messages:
    458
    Likes Received:
    18
    Best Answers:
    0
    Trophy Points:
    110
    #13
    actually we don't know if he is using smarty or not.
    he asked if how to get the value of id and how if there is no id...so he will be in trouble if he use the $_GET sometimes the variables is in the cache or used sessions so we are not sure of what Crayz want really.
     
    rossdalangin, Feb 17, 2007 IP