【Vegas原创】网站计数器(.net) - 心海e站 - 博客园

C# 版:

把下面的代码放到aspnet的Global.asax页面中然后重新编译以下项目

如果文件中有Session_Start(Object sender, EventArgs e)方法,只需要复制代码到该方法最顶部

 

         
protected void Session_Start(Object sender, EventArgs e)

         {

string strCon="Database=tmpCounter;uid=sa;pwd=*****;server=*****";

 

              
string server_name,url;

              url
=Request.ServerVariables["SCRIPT_NAME"].ToString();

              
string [] urlArray=url.Split('/');

              server_name
=Request.ServerVariables["SERVER_NAME"].ToString() + "/" + urlArray[1];

              Application.Lock();

 

              
if(Application["counter"]==null

                   Application[
"counter"]=1;

              
else

                   Application[
"counter"]=int.Parse(Application["counter"].ToString())+1;

 

 

              Application.UnLock();

              

     

              
string login_ip=Request.ServerVariables["REMOTE_ADDR"].ToString();

              
string insStr="insert tcounter(login_ip,server_name,counter) values('" + login_ip + "','" + server_name +"'," + Application["counter"+ ")";

              

              System.Data.SqlClient.SqlConnection db
=new System.Data.SqlClient.SqlConnection(strCon);

              db.Open();

              System.Data.SqlClient.SqlCommand sqlCom
=new System.Data.SqlClient.SqlCommand(insStr,db);

              sqlCom.ExecuteNonQuery();

              db.Close();

              sqlCom.Dispose();

         }


VB.net版:
    Sub Session_Start(ByVal sender As ObjectByVal e As EventArgs)
        
Dim strCon As String
        strCon 
= "Database=tmpCounter;uid=sa;pwd=****;server=*****"



        
Dim server_name As String
        
Dim url As String
        url 
= Request.ServerVariables("SCRIPT_NAME").ToString()
        
Dim urlArray As Array
        urlArray 
= url.Split("/")

        server_name 
= Request.ServerVariables("SERVER_NAME").ToString() & "/" & urlArray(1)

        Application.Lock()



        
If Application("counter"Is Nothing Then

            Application(
"counter"= 1

        
Else

            Application(
"counter"= Convert.ToInt32(Application("counter").ToString()) + 1
        
End If




        Application.UnLock()

        
Dim login_ip As String
        
Dim insStr As String
        login_ip 
= Request.ServerVariables("REMOTE_ADDR").ToString()

        insStr 
= "insert tcounter(login_ip,server_name,counter) values('" & login_ip & "','" & server_name & "'," & Application("counter"& ")"


        
Dim db As System.Data.SqlClient.SqlConnection
        db 
= New System.Data.SqlClient.SqlConnection(strCon)

        db.Open()
        
Dim sqlCom As System.Data.SqlClient.SqlCommand
        sqlCom 
= New System.Data.SqlClient.SqlCommand(insStr, db)

        sqlCom.ExecuteNonQuery()

        db.Close()

        sqlCom.Dispose()


        
' 在会话启动时激发
        'Session("out") = "2002/7/9"
        Session.Timeout = 60
        Session(
"username"= ""
        Session(
"usertype"= ""
        Session(
"operation"= "change"
        
'Session("oldpagecount") = 0

        Application(
"usercount"= Application("usercount"+ 1

    
End Sub
郑重声明:资讯 【【Vegas原创】网站计数器(.net) - 心海e站 - 博客园】由 发布,版权归原作者及其所在单位,其原创性以及文中陈述文字和内容未经(企业库qiyeku.com)证实,请读者仅作参考,并请自行核实相关内容。若本文有侵犯到您的版权, 请你提供相关证明及申请并与我们联系(qiyeku # qq.com)或【在线投诉】,我们审核后将会尽快处理。
—— 相关资讯 ——