View Full Version : Page Access Script Question

Doctor Snake
02-12-2006, 02:44 AM
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?

02-12-2006, 05:41 AM
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:

if (document.referrer!='http://www.somedomain.com/valid.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:

/*Intelligent Back button
* John Davenport Scheuer (jscheuer1)
* as seen in www.dynamicdrive.forums
* this notice must remain for legal use */

function goBack(){

Here is the markup:

<a href="main_1.htm" onclick="goBack();return false;">

02-12-2006, 09:55 AM
However, a lot of firewalls strip the Referer header from HTTP requests, and some users disable it, so this is not a viable option.

02-12-2006, 10:19 AM
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.