天天看點

判斷本頁面通路來源是否為本站連結

判斷本頁面通路來源是否為本站連結:

protected bool IsRefererRight
{
     get
     {
         string s1 = Request.ServerVariables["HTTP_REFERER"];
         string s2 = Request.ServerVariables["SERVER_NAME"];

#if DEBUG
         System.Diagnostics.Trace.WriteLine(s1);//http://localhost:1783/MainPage.aspx
         System.Diagnostics.Trace.WriteLine(s2);//localhost
#endif
         return s1 != null && (s1.IndexOf(s2) == 7);
     }
}      

正确:

Request.ServerVariables["HTTP_REFERER"]==http://localhost:1783/MainPage.aspx

Request.ServerVariables["SERVER_NAME"]==localhost

外部連結:

Request.ServerVariables["HTTP_REFERER"]==http://xxx/yyy.zzz

Request.ServerVariables["SERVER_NAME"]==xxx

非連結,浏覽器直接通路:

Request.ServerVariables["HTTP_REFERER"]==null

Request.ServerVariables["SERVER_NAME"]==xxx