Ziyaretçi İstatistiklerini Almak
Web sayfamızı ziyaret eden ziyaretçilerin hangi sayfalara girdikleri, ip numaralarını, giriş tarihlerini, hangi tarayıcı kullandıklarını kaydedeceğiz. Bunun için Global.asax ta Application_BeginRequest(object sender, EventArgs e) e aşağıdaki kodları yazacağız.
Global.asax
<%@ Import Namespace="System.Data.OleDb" %>
<%@ Application Language="C#" %>
<script runat="server">
protected void Application_BeginRequest(object sender, EventArgs e)
{
string str = "Insert into ziyaretci (url,sayfa,ip,kaynak,tarih) values(@url,@sayfa,@ip,@kaynak,@tarih)";
OleDbConnection baglan = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/App_Data/bilgi.accdb") + "; Persist Security Info=False;");
OleDbCommand komut = new OleDbCommand(str, baglan);
komut.Parameters.AddWithValue("@url", Request.ServerVariables["URL"].ToString());
komut.Parameters.AddWithValue("@sayfa", Request.ServerVariables["Query_String"].ToString());
komut.Parameters.AddWithValue("@ip", Request.ServerVariables["Remote_Addr"].ToString());
komut.Parameters.AddWithValue("@kaynak", Request.ServerVariables["Http_User_Agent"].ToString());
komut.Parameters.AddWithValue("@tarih", DateTime.Now.ToString());
baglan.Open();
komut.ExecuteNonQuery();
baglan.Close();
}
void Application_Start(object sender, EventArgs e)
{
// Code that runs on application startup
}
void Application_End(object sender, EventArgs e)
{
// Code that runs on application shutdown
}
void Application_Error(object sender, EventArgs e)
{
// Code that runs when an unhandled error occurs
}
void Session_Start(object sender, EventArgs e)
{
// Code that runs when a new session is started
}
void Session_End(object sender, EventArgs e)
{
// Code that runs when a session ends.
// Note: The Session_End event is raised only when the sessionstate mode
// is set to InProc in the Web.config file. If session mode is set to StateServer
// or SQLServer, the event is not raised.
}
</script>
Default.aspx
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.OleDb;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
string str = "Select * From ziyaretci";
OleDbConnection baglan = new OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=" + Server.MapPath("~/App_Data/bilgi.accdb") + "; Persist Security Info=False;");
OleDbDataAdapter adp = new OleDbDataAdapter(str, baglan);
OleDbCommand komut = new OleDbCommand(str, baglan);
baglan.Open();
DataSet ds= new DataSet();
adp.Fill(ds);
GridView1.DataSource = ds.Tables[0];
GridView1.DataBind();
baglan.Close();
}
}
Default.aspx.cs
<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs" Inherits="_Default" %>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
<title></title>
</head>
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server" CellPadding="4" ForeColor="#333333"
GridLines="None">
<RowStyle BackColor="#F7F6F3" ForeColor="#333333" />
<FooterStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<PagerStyle BackColor="#284775" ForeColor="White" HorizontalAlign="Center" />
<SelectedRowStyle BackColor="#E2DED6" Font-Bold="True" ForeColor="#333333" />
<HeaderStyle BackColor="#5D7B9D" Font-Bold="True" ForeColor="White" />
<EditRowStyle BackColor="#999999" />
<AlternatingRowStyle BackColor="White" ForeColor="#284775" />
</asp:GridView>
</div>
</form>
</body>
</html>
Bir makalenin daha sonuna geldik. Bir sonraki makalede görüşmek dileğiyle. Hoşçakalın. Bahadır ŞAHİN