meco

log_user_ip_and_see_if_access_to_website_is_true

Mar 16th, 2011
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.86 KB | None | 0 0
  1. protected void Page_Load(object sender, EventArgs e)
  2.     {
  3.  
  4.         // get user ip
  5.         string host = Dns.GetHostName();
  6.         IPHostEntry ip = Dns.GetHostEntry(host);
  7.         string strClientIP = Context.Request.ServerVariables["REMOTE_ADDR"];
  8.         lblUserIP.Text = strClientIP;
  9.  
  10.        
  11.         // connect to database and see if user exsists, and check if he is banned..
  12.         string connection = WebConfigurationManager.ConnectionStrings[0].ConnectionString;
  13.         SqlConnection coN = new SqlConnection(connection);
  14.         SqlCommand sql;
  15.         sql = new SqlCommand("SELECT * FROM tblACESS WHERE ip = '" + lblUserIP.Text + "'", coN);
  16.         coN.Open();
  17.         SqlDataReader readME = sql.ExecuteReader();
  18.        
  19.         if (!readME.HasRows)
  20.         {
  21.             readME.Close();
  22.             sql.CommandText="INSERT INTO tblACESS (ip, date, hits, access) VALUES ('" + lblUserIP.Text + "',getdate(),'1','true')";
  23.             sql.ExecuteNonQuery();
  24.             coN.Close();
  25.         }
  26.         else
  27.         {
  28.             readME.Close();
  29.             sql.CommandText = "SELECT * FROM tblACESS WHERE ip = '" + lblUserIP.Text + "'";
  30.             readME = sql.ExecuteReader();
  31.             int hits = 0;
  32.             bool hasACCESS = true;
  33.             while (readME.Read())
  34.             {
  35.                 hits = (int)readME["hits"];
  36.                 hasACCESS = Convert.ToBoolean(readME["access"]);
  37.    
  38.             }
  39.             hits++;
  40.             readME.Close();
  41.             sql.CommandText = "UPDATE tblACESS SET date = getdate(),  hits = '" + hits.ToString() + "' WHERE ip = '" + lblUserIP.Text + "'";
  42.             sql.ExecuteNonQuery();
  43.             coN.Close();
  44.  
  45.             if (!hasACCESS)
  46.             {
  47.                 Response.Redirect("http://tinyurl.com/69tmmqq");
  48.             }
  49.      
  50.  
  51.            
  52.         }
  53.        
  54.        
  55.  
  56.     }
Add Comment
Please, Sign In to add comment