Get and save visitors information ASPX & AJAX
The number is usually displayed, with image or text, as an old inline digital image, a plain text or an old mechanical counter. Image renderization of digits may use a variety of fonts and styles; the classic example is the wheels of an odometer. The counter is often accompanied by the date it was set up or last reset, otherwise it becomes impossible to estimate within what time the number of page loads counted occurred. (From Wikipedia)
In this blog post I will present you a simple method to obtain data( IP , country code , country name ) from your website visitor using ASP.net and AJAX , Enjoy reading .
Step 1
- We need to create a database to store visitor information( IP , country code , country name )SQL Code
create database tuto4free;
go
use tuto4free;
go
create table siteLog(
ID int NOT NULL IDENTITY primary key,
IP varchar(15),
country_code varchar(5),
country_name varchar(20),
)
Step 2
- Create a static web method whit 3 parameters to insert user information in the databaseC# Code
[System.Web.Services.WebMethod]
[ScriptMethod]
public static void insertlog(string IP,string country_code,string country_name)
{
string SQL = " insert into siteLog(IP,country_code,country_name) values('" + IP + "','" + country_code + "','" + country_name + "') ";
SqlCommand cmd = new SqlCommand(SQL, cnnx);
cnnx.Open();
cmd.ExecuteNonQuery();
}
Step 3
- In the HTML code we well use 2 javascript functions- To get visitor information
- To store visitor information
<script>
//www.tuto4free.com
jQuery.ajax({
url: '//freegeoip.net/json/',
type: 'POST',
dataType: 'jsonp',
success: function (location) {
insert(location.ip, location.country_code, location.country_name)
}
});
function insert(IP, country_code, country_name)
{
$.ajax({
type: "POST",
url: "index.aspx/insertlog",
data: "{'IP':'" + IP + "','country_code':'" + country_code + "','country_name':'" + country_name + "'}",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function (res) {
alert('Welcom');
},
error: function (xhr, status, error) {
alert(error);
}
});
}
</script
That's entirely all what you necessitate to begin get your visitor information
if you have any question do not hesitate to contact me