Click here to Skip to main content
15,901,001 members
Please Sign up or sign in to vote.
3.00/5 (1 vote)
See more:
Hi,
In my local a can get my ip address and insert it in my local db but when uploading files in live
and db I am not able to get my ip address and insert it.means the system does not get my ip address.
I am using the following address:
C#
public string GetIP4Address() {

    string IP4Address = String.Empty;

    foreach (IPAddress IPA in Dns.GetHostAddresses(Request.ServerVariables["REMOTE_ADDR"].ToString()))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }

    if (IP4Address != String.Empty)
    {
      return IP4Address;
    }

    foreach (IPAddress IPA in Dns.GetHostAddresses(Dns.GetHostName()))
    {
      if (IPA.AddressFamily.ToString() == "InterNetwork")
      {
        IP4Address = IPA.ToString();
        break;
      }
    }
    Session["IpAddress"] = IP4Address;
    return IP4Address
};


and I am using a class called UserCartInfo to do the job like this:
C#
public int AddToCart()
   {
       return Convert.ToInt32(SqlHelper.ExecuteScalar(SqlHelper.GetConnection(), "AddToCart", ProductId, UserId, Quantity, ipAddress));
   }


and a generic handler for Json,like this:

C#
public void ProcessRequest (HttpContext context) {
       //if ((HttpContext.Current.Session["UserId"] == null)|(HttpContext.Current.Session["UserId"] != null))
       //{
           UserCartInfo _UserCartInfo = new UserCartInfo();
           _UserCartInfo.ProductId = Convert.ToInt32(context.Request["ProductId"]);
           _UserCartInfo.Quantity =  Convert.ToInt32(context.Request["Quantity"]);
           _UserCartInfo.UserId = Convert.ToInt32(HttpContext.Current.Session["UserId"]);
           _UserCartInfo.ipAddress = Convert.ToString(HttpContext.Current.Session["IpAddress"]);
           context.Response.ContentType = "application/json";

           var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
           HelperFunctions.InitializeCulture();
          var result = new
           {
               Success = _UserCartInfo.AddToCart() == 1 ? false : true,
              // Message = _UserCartInfo.AddToCart() == 1 ? "Cannot add your own product" : "Added to cart"
           };
           var json = serializer.Serialize(result);
           context.Response.Write(json);
       //}
   }


as I said this working in local but not in live I do not where is the problem.Please some help...
Posted

1 solution

The problem is quite simple: when you run this locally, the server and the client are the same computer.
C# code runs on the server, not the client. So when you query the DNS you are requesting information from the server DNS, not the client. So the information you get is server centric and have nothing at all to do with the source of the file.

When you ran it locally, the two werethe same PC, so it didn't matter.

Try getting the client IP address with Request.UserHostAddress instead.

"But as I am working on e-commerce website,you know that if I am adding items in the cart the system must know that this pc has added something.the problem is this when I go to another machine I can see what I have add,while machine are different. and when I add item in the cart by using different pc,I can see that Ip address are same of those machines in my db.Please can u give me an idea on how e-commerce work when a user add something in the cart...because shopping cart are different.Means if I am using another machine I can not see what the other user have added. Help on that"

Right - you do not want to use the IP address at all! Definitely not!

An IP address is assigned to a router (or other internet connection device) and all PCs connecting to the internet via that router will have the same IP address - so if you store the data against an IP address then all the customers in the same company will buy the same huge range of goods - not a good idea, really...

The normal way is either to store them in a Database against a login id (which means that customers must log in before they can shop, which puts many people off) or in Cookies, which are stored on the PC making the purchase and thus are separate.
(You can also store the info in the Session, but that does not persist long - if the browser is closed or the user shops for too long you could loose the info - not a good idea for a shopping site)

Using Cookies is simple: http://msdn.microsoft.com/en-us/library/system.web.httpresponse.cookies.aspx[^] and is the way most shopping sites work.
 
Share this answer
 
v2
Comments
El Dev 6-Mar-13 5:57am    
ok I have changed the code now to get ip address I am using this code bellow,and it is working:
private string GetIP()
{
HttpRequest request = base.Request;
//Get UserHostAddress property
string address = request.UserHostAddress;
//write to response;
return address;
}
But as I am working on e-commerce website,you know that if I am adding items in the cart the system must know that this pc has added something.the problem is this when I go to another machine I can see what I have add,while machine are different. and when I add item in the cart by using different pc,I can see that Ip address are same of those machines in my db.Please can u give me an idea on how e-commerce work when a user add something in the cart...because shopping cart are different.Means if I am using another machine I can not see what the other user have added.
Help on that
OriginalGriff 6-Mar-13 6:12am    
Answer updated
El Dev 6-Mar-13 6:20am    
am I need to store the cookies in the db as you see the idea I am using to store the data in the db using ip address.I have visited the link you send me but I do not understand hw should I associate it to the shopping cart I have while adding items.
OriginalGriff 6-Mar-13 6:27am    
No - cookies are stored by the client browser on the client PC.
So even if they turn the PC off and come back to your site tomorrow, you read the cookies at the server and Presto! You have the shopping list for that PC back.

Basically, the Cookie is the shopping cart. You do not have to store anything in your DB (which is good, because otherwise you have to removed "dead" cart entries at some point or your DB fills up with rubbish) - it is all held by the customer, with the customer. You can even say "this cookie expires after 14 days" or whatever to prevent them clogging their lists up, the their browser will handle expiration for you.

Ignore the IP address - it will not help you at all. Not only is it shared by all users on the same router, but most consumer ISPs allocate IP addresses dynamically: if the customer turns off the router, he will get a different IP address when he turns it back on again - and a different shopping list from your website!
El Dev 6-Mar-13 6:34am    
ok,Please can u help on how I can store the items in the cookies because I was using this code to do the job by using JSON,like this:
public void ProcessRequest (HttpContext context) {
//if ((HttpContext.Current.Session["UserId"] == null)|(HttpContext.Current.Session["UserId"] != null))
//{
UserCartInfo _UserCartInfo = new UserCartInfo();
_UserCartInfo.ProductId = Convert.ToInt32(context.Request["ProductId"]);
_UserCartInfo.Quantity = Convert.ToInt32(context.Request["Quantity"]);
_UserCartInfo.UserId = Convert.ToInt32(HttpContext.Current.Session["UserId"]);
_UserCartInfo.ipAddress = Convert.ToString(HttpContext.Current.Session["mycookie"]);
context.Response.ContentType = "application/json";

var serializer = new System.Web.Script.Serialization.JavaScriptSerializer();
HelperFunctions.InitializeCulture();
var result = new
{
Success = _UserCartInfo.AddToCart() == 1 ? false : true,
// Message = _UserCartInfo.AddToCart() == 1 ? "Cannot add your own product" : "Added to cart"
};
var json = serializer.Serialize(result);
context.Response.Write(json);
//}
}
as u see I am getting the info and insert it into a db but as u said this is not a better,
now what I want is that code by using cookies.How to perform that using cookies.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900