Results 1 to 4 of 4

Thread: Page Access Script Question

  1. #1
    Join Date
    Feb 2006
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Page Access Script Question

    I am looking for a script that will look at the "referring" page and if it is not a specific page, the user will be directed to another page. (in simple terms if you attempt to acces page X from anywhere other than page Z you are directed to page Y) - Any thoughts?

  2. #2
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    The referring page can be accessed via document.referrer. This property will contain the address of the page that the user was on when they clicked a link that brought them to the current page. If they got to the current page using the back or forward buttons on the browser, it will contain the address of the page that they were on originally that linked them to the current page. If they get to the current page by typing its address into the browser or via a bookmark, the referrer is an empty string:

    Code:
    if (document.referrer!='http://www.somedomain.com/valid.htm')
    window.location.replace('other.htm')
    Something like the above should do the trick. Here is some actual code for a back button I wrote that determines if the user came from any other page on the current domain and to direct them back to it if they did, to a set page on the domain if they did not:

    Code:
    /*Intelligent Back button
    * © John Davenport Scheuer (jscheuer1)
    * as seen in www.dynamicdrive.forums 
    * this notice must remain for legal use */
    
    function goBack(){
    if(document.referrer.indexOf(location.hostname)==7)
    history.go(-1);
    else
    window.location.href='main_1.htm';
    }
    Here is the markup:

    HTML Code:
    <a href="main_1.htm" onclick="goBack();return false;">
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

  3. #3
    Join Date
    Jun 2005
    Location
    英国
    Posts
    11,876
    Thanks
    1
    Thanked 180 Times in 172 Posts
    Blog Entries
    2

    Default

    However, a lot of firewalls strip the Referer header from HTTP requests, and some users disable it, so this is not a viable option.
    Twey | I understand English | 日本語が分かります | mi jimpe fi le jbobau | mi esperanton komprenas | je comprends français | entiendo español | tôi ít hiểu tiếng Việt | ich verstehe ein bisschen Deutsch | beware XHTML | common coding mistakes | tutorials | various stuff | argh PHP!

  4. #4
    Join Date
    Mar 2005
    Location
    SE PA USA
    Posts
    30,495
    Thanks
    82
    Thanked 3,449 Times in 3,410 Posts
    Blog Entries
    12

    Default

    I agree with Twey on that, mostly out of ignorance. However, anything javascript is subject to the client turning off javascript, so any use of it for navigation should have an HTML backup plan, like my little back button script and markup do. For your purposes perhaps you could have a server side backup. It also depends upon how critical this all is. If you are just trying to provide a convenience to the majority of users that will not have the limitations Twey mentions, it is fine. As long as nothing terrible will happen if someone navs into your page without having come from the preferred location, it is fine. You could provide guidance for these unfortunate souls in the form of some text explaining where they should go first for the optimal experience of the page. Perhaps a more sophisticated method of fall back could be devised as well. Like at least if they did come from the preferred referrer and it can be verified by javascript, some code could make the explanation text's display property none or its visibility invisible.
    - John
    ________________________

    Show Additional Thanks: International Rescue Committee - Donate or: The Ocean Conservancy - Donate or: PayPal - Donate

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
  •