【Vegas原创】网站计数器(.net)

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

 

posted @ 2007-03-16 10:04  李济宏(Amadeus)  阅读(167)  评论(0编辑  收藏  举报