如何使用c#在SQL中获取和存储我的网站访问者的IP地址 [英] How to get and store the ip address of visitors of my Website in SQL using c#

查看:84
本文介绍了如何使用c#在SQL中获取和存储我的网站访问者的IP地址的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取我网站访问者的IP地址。我知道可以使用



string ipaddress;

ipaddress = Request.ServerVariables [" HTTP_X_FORWARDED_FOR"];

if(ipaddress =="" || ipaddress == null)

ipaddress = Request.ServerVariables [" REMOTE_ADDR"];



但我不知道我想把它放在哪里。在哪个页面母版页或Default.aspx页面。如何将这些细节存储在数据库中。我想检查它有多少他们正在访问我们的网站....任何人都可以给出完整的图片。

I want to get the ip address of the visitors of my website. I know it can be done using

string ipaddress;
ipaddress = Request.ServerVariables["HTTP_X_FORWARDED_FOR"];
if (ipaddress == "" || ipaddress == null)
ipaddress = Request.ServerVariables["REMOTE_ADDR"];

But i d't know where i want to put it.In which page master page or Default.aspx page.And how can i store this details in the database.And i want to check it how many of them are visiting our site ....Anyone can give acomplete picture of this.

推荐答案

http://forums.asp.net/t/1397341.aspx [ ^ ]



查看此链接..希望它有帮助..
http://forums.asp.net/t/1397341.aspx[^]

Check this link..hope it will help..


如果您的站点为每个用户使用Session(即登录),那么您可以将持久性逻辑放在
If your site uses Session for each user (i.e, login), then you can place the persistance logic in
Global.asax/Session_start 

方法。



如果你没有使用它,那么你可以创建一个基类并在那里有逻辑。

因此,对于您希望此功能从页面继承而不是从System.Web.UI.Page继承的页面。

method.

If you are not using it, then you can create a base class and have the logic there.
So for pages where you want this functionality inherit from base page instead of inheriting from System.Web.UI.Page.


假设您想获取详细信息访问过您网站中的联系页面的用户。然后,在联系页面的 Page_Load 事件中,只需输入如下一行:

Suppose you want to get details of users who visited the contact page in your website. Then,in Page_Load event of the contact page just put the single line as below:
protected void Page_Load(object sender, EventArgs e)
{
     if (!IsPostBack)
     {
           string ip = Server.HtmlEncode(Request.UserHostAddress);
     }
}



这会给你ip作为字符串。您可以随时随地将其插入数据库。



注意:



您将获得127.0.0.1每次如果你从localhost运行它。



问候。:)


This will give you ip as a string. You can insert it into database wherever you want.

Note:

You will get 127.0.0.1 every time if you run it from localhost.

Regards.:)


这篇关于如何使用c#在SQL中获取和存储我的网站访问者的IP地址的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆