Hii Holmes,
suppose that ,you have a page say "http://home.cogeco.ca/~grimreaper/CriticalPage1.html"
This page should be visible only for login user or some referal sites.
Now think you want to allow only those users who come from the "referal_list"
i.e("http://www.xyzabctemp1.com","http://www.xyzabctemp2.com","http://www.xyzabctemp1010.com")
Any user who is coming from other site say "http://vinod_yadav1919test.com/test.asp" should not have
access to your site.
May be the owner of vinod_yadav1919test.com, put a link to your site by just adding a href tag,so that
other users can access your sites(Note even in this time Vinod_yadav1919test.com is not a valid referal site) and i am puting the Code in my "test.asp" page like this
<a href="http://home.cogeco.ca/~grimreaper/CriticalPage1.asp">Visit Holmes Site</a>
Now when user clicks the link to above "Visit Holmes Site", your url will be in the user address bar,
then your javascript function check from which sites the request is made.
In this case value of the variables willbe
document.referer will be (
http://vinod_yadav1919test.com/test.html)
i.e
"from"->
http://vinod_yadav1919test.com/test.html [Originating site of the request]
"domainname"->"vinod_yadav1919test.com"
1) You do not need to put in the "from" variable,
It is the url of the Referer page(May be it is your given referral site or some other site)
2)You do not need to put "http://home.cogeco.ca/~grimreaper/" in the array
Basically referal_list is the name of all the sites to whom you allowed to see/access your pages
3)Any other sites which are not in the "referal_list" , will directly move to your login page
from=document.referrer
var referal_list=new Array("xyzabctemp1.com","xyzabctemp2.com","xyzabct emp1010.com")
location.href="http://home.cogeco.ca/~grimreaper/loginpage.html
Please put the comment for alert(flag),i forgot to comment it :(
In More Depth?
Save below code as "test.html"
*************************** test.html start**************
<script>
function IsValidReferral()
{
from=document.referrer
domainname=from.substring(from.indexOf("//")+2,from.length)
domainname=domainname.substring(0,domainname.index Of("/"))
var referal_list=new Array("xyzabctemp.com","xyzabctemp1.com","xyzabcte mp2.com");
flag=false;
for(i=0;i<referal_list.length;i++)
{
if(domainname==referal_list[i])
{flag=true
break;
}
}
if(!flag)
{alert("You are Invalid User"+from)
//It's not a referral site,Move to your login Page
location.href="http://home.cogeco.ca/~grimreaper/"
}
else
{alert("success")
}
}
IsValidReferral();
</script>
*********************test.html end **********************
and Make a new page "check.html" and put a link
******************check.html start**********
<a href="http://home.cogeco.ca/~grimreaper/test.html">Test the Site</a>
****************check.html end*********
NOw ,Try to open your page by in the browser
http://home.cogeco.ca/~grimreaper/check.html and click the the Link on that page
Cheers :)
vinod