SitePoint Sponsor

User Tag List

Results 1 to 17 of 17
  1. #1
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    I want to check if the referer is from a certain domain

    What I want to do: When a user views the page, I want to check whether they're coming from another page on my site or whether this is the first page that they're viewing. If it's the first page, then I want to echo some code to the browser (it will read the cookie file for my site)

    This is the code that I tried to use:
    Code:
    $refp = parse_url($HTTP_REFERER);
    $refp = $refp[host];
    
    if ($refp != "rideau.prowsej.com")
    {
    echo "insert javascript here that the browser will execute";
    }
    - My domain is rideau.prowsej.com
    - The above code didn't seem to work.

    It seemed that a lot of the time, the referer was null. Under what conditions would the referer be null (where could I read about the $HTTP_REFERER variable?)

    Any reply is appreciated.

    - Joshua Prowse
    Rideau High School Student

  2. #2
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Try this:
    PHP Code:
    if(eregi("rideau\\.prowsej\\.com"$HTTP_REFERER) {
           
    # if the visitor came from your site
    } else {
           
    # if they didn't

    Last edited by qslack; Jul 24, 2001 at 01:13.

  3. #3
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks for the code, it appears to work. Now, I have a question about the $HTTP_REFERER variable.

    • If I am at rideau.prowsej.com and I click on a link that takes me to another page on the same domain, then the $HTTP_REFERER shows that I've come from rideau.prowsej.com
    • If I am at rideau.prowsej.com and I click on a bookmark in my browser that takes me to a page on the same domain, then the $HTTP_REFERER doesn't show that I've come from rideau.prowsej.com
    • If I am at rideau.prowsej.com in my browser and I manually type the URL in my browser that takes me to a page on the same domain, then the $HTTP_REFERER doesn't show that I've come from rideau.prowsej.com


    Essentially, I was expecting $HTTP_REFERER to be the server-side equivalent of Javascript's history property - always indicating the previous page that the user was at. Instead it only seems to work if the user clicks on a link and not if they use a browser function (history, location bar) to go to the new page.

    Would there be anything server-side that would do more of what I want or would this be considered a privacy invasion to send all of that information to the server?

  4. #4
    Mlle. Ledoyen silver trophy seanf's Avatar
    Join Date
    Jan 2001
    Location
    UK
    Posts
    7,168
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    $HTTP_REFERER tells you what page you came from, that is why it is blank from bookmarks or when directly typed

    Sean
    Harry Potter

    -- You lived inside my world so softly
    -- Protected only by the kindness of your nature

  5. #5
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    What seanf says is right

    Which is why if you hit refresh $HTTP_REFERER will also be null which is annoying for testing.

    Adding to what qslack says you could do

    Code:
    if ($HTTP_REFERER){
    if(eregi("rideau\.prowsej\.com", $HTTP_REFERER) {
           # if the visitor came from your site
    } else {
           # if they didn't
    }
    }
    Not very secure, but good if you are going to be testing your site a lot, you can allways change it back when your script / page is done.
    Last edited by MPK; Jul 24, 2001 at 13:13.

  6. #6
    :) delemtri's Avatar
    Join Date
    Jun 2001
    Posts
    579
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I've got a question!

    If I have in an HTML file <img src="picture.php?blah"> and was using HTTP_REFERER in picture.php, what would HTTP_REFERER hold - the same as in the HTML file, or the HTML filename itself?

  7. #7
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by delemtri
    I've got a question!

    If I have in an HTML file <img src="picture.php?blah"> and was using HTTP_REFERER in picture.php, what would HTTP_REFERER hold - the same as in the HTML file, or the HTML filename itself?
    the HTTP_REFERER of a picture is (should) be the HTML page it was called from.

    hey, why isn't it spelled REFERRER? hmmm...

  8. #8
    SitePoint Wizard
    Join Date
    Jul 1999
    Location
    Chicago
    Posts
    2,629
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It's "referer" instead of "referrer" because of an error in the original HTTP standard.


  9. #9
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    This makes me wonder:

    When I look at server logs for my site, I've frequently seen the referrer listed as "about:blank". I doubt that many people are editing their about:blank page to include links, so there must be some way of determining the previous page that the user was on, even if they used a browser function like a bokmark or refresh command to come to your site.

  10. #10
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    i just tried it accessing a Perl script from IE 5 & 5.5 and if i refresh the page (hard even) it still shows the HTTP_REFERER.

  11. #11
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yes, HTTP REFERER stays when you press refresh, therefore, if you go directly to a script to test it, then every time you refresh it will think the same.

    This is what I actually use in my script when finished.

    PHP Code:
    function checkhttp() {
    global 
    $HTTP_REFERER;
    $tofind "mydomain.com";
    $result strstr($HTTP_REFERER,$tofind);
    if (
    $result == false) { 
    echo 
    "<font color=red>Sorry this form can only be filled in from this site.</font>";
    somefunction();
    } else {
    someotherfunction();
    }

    Using $tofind may seem pointless, but means you can have it in your .inc so if you are making a commercial script.

    BTW, is it better to use eregi?

  12. #12
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by DR_LaRRY_PEpPeR
    i just tried it accessing a Perl script from IE 5 & 5.5 and if i refresh the page (hard even) it still shows the HTTP_REFERER.
    What is the difference between a hard and soft refresh in IE?
    Would a soft refresh be pressing F5?
    Would a hard refresh be pressing CTRL+R?

  13. #13
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    A hard refresh is CTRL + F5, aka server refresh.

  14. #14
    SitePoint Addict
    Join Date
    Jan 2001
    Location
    Ottawa ON
    Posts
    315
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by MPK
    A hard refresh is CTRL + F5, aka server refresh.
    It appears that in IE5:
    CTRL+R = CTRL+F5 + Resetting Address Bar

    (If you delete the address in the address bar, pressing F5 would leave it blank, CTRL+R doesn't)

  15. #15
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by prowsej
    What is the difference between a hard and soft refresh in IE?
    Would a soft refresh be pressing F5?
    Would a hard refresh be pressing CTRL+R?
    i have always called it hard when i do Shift + click refresh.

  16. #16
    SitePoint Zealot
    Join Date
    May 2001
    Location
    UK
    Posts
    184
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Shift + F5 doesn't do anything. So not sure shift + pressing refresh would.

  17. #17
    Making a better wheel silver trophy DR_LaRRY_PEpPeR's Avatar
    Join Date
    Jul 2001
    Location
    Missouri
    Posts
    3,428
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Originally posted by MPK
    Shift + F5 doesn't do anything. So not sure shift + pressing refresh would.
    yeah i know w/ F5 it doesn't, but clicking Refresh it does. that's the way i've always heard to do it.

    it seems to work cuz if you're scrolled down from the top of a page and click Refresh the page will reload at the same position. using Shift + Refresh will reload scolled back up to the top - just as when it's first loaded.
    - Matt ** Ignore old signature for now... **
    Dr.BB - Highly optimized to be 2-3x faster than the "Big 3."
    "Do not enclose numeric values in quotes -- that is very non-standard and will only work on MySQL." - MattR


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
  •