1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How do I find the URL of the previous page?

Discussion in 'PHP' started by Weirfire, Oct 19, 2004.

  1. #1
    I was wondering if there was a way of extracting the page that was last viewed with PHP? It's only for pages where someone has clicked through to the page.
     
    Weirfire, Oct 19, 2004 IP
  2. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #2
    I found it.

    It's $_SERVER['HTTP_REFERRER'] just in case anyone was curious. :)
     
    Weirfire, Oct 19, 2004 IP
  3. xml

    xml Peon

    Messages:
    254
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #3
    <?php
    $ref = getenv("HTTP_REFERER") ;
    ?>
     
    xml, Oct 19, 2004 IP
  4. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #4
    Is there any advantage of using getenv than $_SERVER. They seem to do the same thing.
     
    Weirfire, Oct 19, 2004 IP
  5. xml

    xml Peon

    Messages:
    254
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #5
    Hmm not that i know of :).
     
    xml, Oct 19, 2004 IP
  6. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #6
    lol

    Cheers :)
     
    Weirfire, Oct 20, 2004 IP
  7. T0PS3O

    T0PS3O Feel Good PLC

    Messages:
    13,219
    Likes Received:
    777
    Best Answers:
    0
    Trophy Points:
    0
    #7
    I believe $_SERVER is how they want you to do it. getenv won't be supported for much longer. It has to do with global settings etc. Used to be a security risk but with $_SERVER $_GET etc. is fixed apparently. (Disclaimer: all off the top of my head from research some months back. Do not take this for fact and rely on it.)
     
    T0PS3O, Oct 20, 2004 IP
  8. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #8
    I see. I'll use $_SERVER then :)
     
    Weirfire, Oct 20, 2004 IP
  9. Lever

    Lever Deep Thought

    Messages:
    1,823
    Likes Received:
    94
    Best Answers:
    0
    Trophy Points:
    145
    #9
    So how can this be executed from within the code of the page? I've got a page that processes a form and I want to enable it for the user to be able to click "go back" when a form field is missing. This is in my php
    if(empty($email)) {
    	echo "<p>You didn't enter an email address, please go back and try again</p>";
    }
    Code (markup):
    I'd like the go back text to be the link to the referring page (the form)

    Any help very much appreciated :)
     
    Lever, Feb 5, 2005 IP
  10. sadcox66

    sadcox66 Spirit Walker

    Messages:
    496
    Likes Received:
    16
    Best Answers:
    0
    Trophy Points:
    0
    #10
    if(empty($email)) {
    	echo "<p>You didn't enter an email address, "."<a href=".$_SERVER['HTTP_REFERRER'].">please go back</a> and try again</p>";
    }
    Code (markup):
     
    sadcox66, Mar 2, 2005 IP
  11. Lever

    Lever Deep Thought

    Messages:
    1,823
    Likes Received:
    94
    Best Answers:
    0
    Trophy Points:
    145
    #11
    Cheers dude, I dropped an R from REFERRER and it worked fine :) Now I just gotta figure out how to keep the variables intact on the previous form page so I've used
    <a href='javascript:history.go(-1)'>please go back</a>
    Code (markup):
    to keep it working for now ;)
     
    Lever, Mar 3, 2005 IP
  12. walliioo

    walliioo Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #12
    There is a big problem using HTTP referer with IE ... this function simply do not send the URL ... it works with Mozilla but not with IE ...

    So if someone have an other idea ... exept using a form .... it will be fun !!

    Thanks :D
     
    walliioo, Jun 7, 2005 IP
  13. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #13
    Welcome to the forums walliioooioiooooo :)

    The HTTP Referrer works for me with IE. I don't really know how it couldn't work with 1 browser and not another due to the fact it is a PHP function which is executed on the server.

    My feelings are, it's your server that is at fault if you have your code laid out nicely.
     
    Weirfire, Jun 7, 2005 IP
  14. walliioo

    walliioo Peon

    Messages:
    2
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #14
    Yep i did not think about this... may be a security on the server or on the IE ???

    Please help :confused: :confused:
    lol
     
    walliioo, Jun 8, 2005 IP
  15. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Two comments:
    - As already mentioned, it's $_SERVER['HTTP_REFERER'] (It is spelled wrong)
    - This variable is set by the client software and can be manipulated, don't rely on it for anything important.
    You can put them in a $_SESSION variable.
     
    exam, Jun 12, 2005 IP
  16. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #16
    Sorry exam :eek:
     
    Weirfire, Jun 12, 2005 IP
  17. exam

    exam Peon

    Messages:
    2,434
    Likes Received:
    120
    Best Answers:
    0
    Trophy Points:
    0
    #17
    No apologies necessary Weirfire, I was only clarifying for archival purposes as I remember all to well when I was starting out and couldn't get it to work until I misspelled it. No worries man :)

    Edit: Oh and I didn't mean you misspelled it, referrer is misspelled as referer in php and in the HTTP protocol.
     
    exam, Jun 12, 2005 IP
  18. Weirfire

    Weirfire Language Translation Company

    Messages:
    6,979
    Likes Received:
    365
    Best Answers:
    0
    Trophy Points:
    280
    #18
    I see :)

    If you use phpinfo(); you get to see all the global variables. :)
     
    Weirfire, Jun 13, 2005 IP
  19. nevetS

    nevetS Evolving Dragon

    Messages:
    2,544
    Likes Received:
    211
    Best Answers:
    0
    Trophy Points:
    135
    #19
    There are sometimes browser objects, toolbars, etc. that block the referrer. It's sent as part of the http request from the client.

    Another way to do things is to set a cookie with every page visit to the location of the page, but that's blockable too.
     
    nevetS, Jun 13, 2005 IP
  20. cornelius

    cornelius Peon

    Messages:
    206
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #20
    isnt refferer unreliable? and cookies be blocked??

    use sessions its easy

    i would do something like

    --------------------------------------------------------------------
    page_1.php
    --------------------------------------------------------------------
    <?php
    session_start();
    
    //some code
    
    $page_name = $_SERVER[PHP_SELF];
    
    $_SESSION['page_name'] = $page_name;
    
    //some code
    ?>
    ------
    Code (markup):
    --------------------------------------------------------------


    --------------------------------------------------------------------
    page_2.php
    --------------------------------------------------------------------
    <?php
    session_start();
    
    //some code
    
    $previous_page = $_SESSION['page_name'];
    
    //some code
    ?>
    ---
    Code (markup):
    -----------------------------------------------------------------
     
    cornelius, Jun 20, 2005 IP