Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Where Visitors Came From

    I have code in my 404 page which uses $_SERVER['HTTP_REFERER'] to catch the link that was requested and inserts in into my database so that I can review exactly what broken links people are using.

    My problem is that I would like to detect the name of the PAGE that the user came from as well as the LINK that they were looking for.

    For example:

    I have a page called test.php and it contains a link to http://www.site.com/nosuchpage. The $_SERVER['HTTP_REFERER'] code in the 404 page returns http://www.site.com/nosuchpage. I would also like to be able to record the PAGE that the user came from ie. test.php.

    ... Hope that that makes sense, let me know if I have not been clear.

    Thanks
    eTheory - the theory of revolution

  • #2
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts

    Re: Where Visitors Came From

    Originally posted by Candrias77
    I have a page called test.php and it contains a link to http://www.site.com/nosuchpage. The $_SERVER['HTTP_REFERER'] code in the 404 page returns http://www.site.com/nosuchpage. I would also like to be able to record the PAGE that the user came from ie. test.php.

    ... Hope that that makes sense, let me know if I have not been clear.

    Thanks
    HTTP_REFERER will give you the page the user came from not the page they were looking for. If you want the page they were looking for then you would use REQUEST_URI. For example:

    PHP Code:

    <?php 
    echo "The requested page was: " $_SERVER["REQUEST_URI"] . "<br>";
    echo 
    "The referring page was: " $_SERVER["HTTP_REFERER"];
    ?>
    You may also want to parse both of those after getting them to strip out everything except for the filename unless of course you want path information.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster

  • #3
    Regular Coder
    Join Date
    Jul 2002
    Location
    New Zealand
    Posts
    127
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Re: Re: Where Visitors Came From

    Originally posted by Spookster
    HTTP_REFERER will give you the page the user came from not the page they were looking for. If you want the page they were looking for then you would use REQUEST_URI.
    This is what I thought but in practice it is not working for me. I definately get http://www.site.com/nosuchpage (the linked url) when i echo $_SERVER['HTTP_REFERER']. HOWEVER, if I then refresh the 404 page it echoes the referring page, http://www.site.com/test.php

    As mentioned, the code resides in my 404 page (not the host's default). Could it be possible that the method my host is using to redirect a user to the custom 404 is causing the incorrect results?
    eTheory - the theory of revolution

  • #4
    Supreme Overlord Spookster's Avatar
    Join Date
    May 2002
    Location
    Marion, IA USA
    Posts
    6,280
    Thanks
    4
    Thanked 83 Times in 82 Posts
    Assuming you are on a Linux server running Apache and you have your 404 page location defined in your .htaccess file then no your host is not doing anything. Apache reads your .htaccess file and anytime a request to Apache ends up with a code 404 then (either redirects to the 404 page you defined or replaces the contents of the missing page with the contents of the 404 page).
    Last edited by Spookster; 04-22-2003 at 07:51 AM.
    Spookster
    CodingForums Supreme Overlord
    All Hail Spookster


  •  

    Posting Permissions

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