判断本页面访问来源是否为本站链接

判断本页面访问来源是否为本站链接:

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

你可能感兴趣的:(判断本页面访问来源是否为本站链接)