Retrieving URL and $_GET values

Discussion in 'PHP' started by mcfc4eva, Jan 11, 2011.

  1. #1
    I'm having trouble retrieving a dyamic URL with two $_GET values.

    One of the $_GET values is the page number so a typical URL will look like this;

    I want to retrieve the whole URL without the page number!

    If I use "$_SERVER['PHP_SELF']" then it doesn't retrieve the value of "value1" and the result is this:

    I tried to use "$_SERVER['REQUEST_URI']" which got the whole URL below, but I don't want the "&page=1" bit.

    I need the URL so I can add the page number at the end depending on what the user selects and so far I have found this solution;

    
    $myvalue = $_GET['value1'];
    $viewurl = $_SERVER['PHP_SELF']."?value1=".$myvalue;
    PHP:
    This solution gives me the URL minus the page number in the "$viewurl" variable, and it works fine, but is it the best/correct way of doing it?

    Regards,
    --Mike
     
    Last edited: Jan 11, 2011
    mcfc4eva, Jan 11, 2011 IP
  2. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #2
    Use $_SERVER['REQUEST_URI'] instead.
     
    ssmm987, Jan 11, 2011 IP
  3. mcfc4eva

    mcfc4eva Well-Known Member

    Messages:
    602
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    110
    #3
    Thanks for your input, but did you even bother reading my post? If you did then you would know that I tried that and didn't get the result I wanted.
     
    mcfc4eva, Jan 11, 2011 IP
  4. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #4
    I dont see your GET call for page ?
    
    $myvalue = $_GET['value1'];
    $mypage = $_GET['page'];
    $viewurl = $_SERVER['PHP_SELF']."?value1=".$myvalue."&page=".$mypage;
    
    PHP:
     
    MyVodaFone, Jan 11, 2011 IP
  5. mcfc4eva

    mcfc4eva Well-Known Member

    Messages:
    602
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    110
    #5
    That's because I don't want to GET the page.
    :)
     
    mcfc4eva, Jan 11, 2011 IP
  6. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #6
    opps then its
    
    $myvalue = $_GET['value1'];
    
    $viewurl = "http://".$_SERVER['SERVER_NAME']."".$_SERVER['PHP_SELF']."?value1=".$myvalue;
    
    
    PHP:
     
    MyVodaFone, Jan 11, 2011 IP
  7. mcfc4eva

    mcfc4eva Well-Known Member

    Messages:
    602
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    110
    #7
    Why do I need "http://" and "$_SERVER['SERVER_NAME']" if it works fine without it?
     
    mcfc4eva, Jan 11, 2011 IP
  8. mcfc4eva

    mcfc4eva Well-Known Member

    Messages:
    602
    Likes Received:
    7
    Best Answers:
    0
    Trophy Points:
    110
    #8
    I think I figured this out for myself.

    The whole purpose of extracting part of the current URL is to input it into a hyper-link. If I include the two values in the quote above then it will give me a <b>absolute URL<b>. But as the script is only used to generate hyper-links within the same domain/server, excluding the above two values would simply make the "$viewurl" value into a relative URL. For this case it doesn't make any difference, but if anyone reading this wants to make external hyperlinks - make sure you use MyVodaFone's version.

    Thanks MyVodaFone. :)

    --Mike
     
    mcfc4eva, Jan 11, 2011 IP
  9. Tanya Roberts

    Tanya Roberts Active Member

    Messages:
    250
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    75
    #9
    If you get any problem then just type:

    print_r($_SERVER); // to get each and every server request
    print_r($_GET); // to get each and every GET type request
    print_r($_POST); // to get each and every POST type request

    similarly $_FILE also works. :D

    They all print the data in array form enabling you to get every information you need while operating.
     
    Tanya Roberts, Jan 12, 2011 IP