One of the important functionalities that you would want your web application have is to detect the capabilities of and information about the browsers visiting your website. With this functionality, you can keep a history of what pages are very popular to your users and where they are coming from. In this article, I will show you how easy it is to retrieve pertinent information about the browsers that are requesting pages from your website.
Setting up the database
The first thing you need to know is how want to retrieve the information from your site. You may write a code that sends to your email the information or save the data to the database. The former may flood your Inbox so we will just the data to the database. Saving the data to the database allows you to perform analysis later. So let’s get started and create the database. Creating the database in the SQL Server is very trivial. Once you have the database created, you can right-click on the Tables folder and start creating the columns. Once the columns have been created, you have to save the Table as Referrer. Refer to the following SQL script when creating the table. You may also want to execute this script to easily create the Referrer table.
CREATE TABLE [dbo].[Referrer](
[ID] [int] IDENTITY(1,1) NOT NULL,
[Url] [nvarchar](256) NOT NULL,
[Browser] [nvarchar](256) NOT NULL,
[IPAddress] [nvarchar](256) NOT NULL,
[Hostname] [nvarchar](256) NOT NULL,
[DateCreated] [datetime] NOT NULL,
CONSTRAINT [PK_Referrer] PRIMARY KEY CLUSTERED
(
[ID] ASC
)WITH (PAD_INDEX = OFF, STATISTICS_NORECOMPUTE = OFF, IGNORE_DUP_KEY = OFF, ALLOW_ROW_LOCKS = ON, ALLOW_PAGE_LOCKS = ON) ON [PRIMARY]
) ON [PRIMARY]
Using SqlDatasource
ASP.NET comes with a Data control named SqlDatasource that allows us to quickly connect to the database to perform various operations such as Insert, Delete, Update and Retrieve. The following code shows how to declare the SqlDatasource in a web form.
<asp:SqlDataSource ID="SqlDataSource1" runat="server"
ConnectionString="<%$ ConnectionStrings:ConnectionString %>"
DeleteCommand="DELETE FROM [Referrer] WHERE [ID] = @ID"
InsertCommand="INSERT INTO [Referrer] ([Url], [Browser], [Hostname], [IPAddress], [DateCreated]) VALUES (@Url, @Browser, @Hostname, @IPAddress, @DateCreated)"
SelectCommand="SELECT [ID], [Hostname], [IPAddress], [DateCreated] FROM [Referrer]"
UpdateCommand="UPDATE [Referrer] SET [Hostname] = @Hostname, [IPAddress] = @IPAddress, [DateCreated] = @DateCreated WHERE [ID] = @ID">
</asp:SqlDataSource>
Notice the ConnectionString property of the SqlDataSource control. The value of this property must be declared in the web.config file as shown below.
<connectionStrings>
<add name="ConnectionString" connectionString="Data Source=.\SQLEXPRESS;AttachDbFilename=|DataDirectory|\Database.mdf;Integrated Security=True;User Instance=True"
providerName="System.Data.SqlClient" />
</connectionStrings>
Now, notice that there are four SqlCommands properties that you can use namely DeleteComnand, InsertCommand, SelectCommand and UpdateCommand. For completeness of presentation, I assigned values to those commands but we will only use the InsertCommand to achieve our objectives in this article.
Using The HttpRequest and HttpBrowserCapabilties
Whenever a page is requested an instance of HttpRequest is instantiated which allows us, among other things, to retrieve pertinent information about the requesting device such as IP address and host name of the device. The HttpRequest object has a Browser properties which can be used to get the information about the capabilities of the requesting device’s browser such as the name of the browser and its major and minor versions. The following code shows an example of retrieving information from the HttpRequest object and saving such information to the database.
protected void Page_Load(object sender, EventArgs e)
{
this.SqlDataSource1.InsertParameters.Add("Url", Request.Url.AbsoluteUri);
this.SqlDataSource1.InsertParameters.Add("Browser", Request.Browser.Type);
this.SqlDataSource1.InsertParameters.Add("Hostname", Request.UserHostName);
this.SqlDataSource1.InsertParameters.Add("IPAddress", Request.UserHostAddress);
this.SqlDataSource1.InsertParameters.Add("DateCreated", DateTime.Now.ToString());
this.SqlDataSource1.Insert();
}
For simplicity, I placed the code inside the Page_Load method which calls the Insert method of the SqlDataSource1. Notice that the SqlDataSource1 has a property for InsertParameters which you can use to set the default value of each column.
Extending the sample code
There are many scenarios in which you can use the sample code presented on this article. Once of which is to get the legitimate hits of a page from a particular device within an allowed period. This means you will modify the InsertCommand ‘s SQL scripts to check whether or not you can again insert the hit from a device that has IP address and host name already existing in the database.
In conclusion
This article presents you a simple but useful code that allows you to get request information from your website users. Hopefully, it will become useful to you when you need to write a code that monitors users’ visits to your website.