多个网站应用程序访客计数器

发布于 2024-09-07 10:14:52 字数 414 浏览 5 评论 0原文

我有一个多个网站 asp.net 应用程序。 在此应用程序中,不同的域使用相同的页面。 所有页面都继承自名为:PageBase 的基类 继承自System.Web.UI.Page。 通过使用: HttpContext.Current.Request.ServerVariables["HTTP_HOST"] 我确定域名是什么,然后获取我需要的所有信息 对于这个域,一切都运行良好。

当我想根据会话为每个站点使用不同的访问者计数器时,我的问题就开始了。 因为 Global.asax 有全局事件: 会话开始 会话结束 简单的计数器将所有网站上的所有访问者一起统计。 我尝试在不同的类中为 Global.asax 编写代码 但我没有在该类中获取 PageBase(System.Web.UI.Page) 网站信息。

我将非常感谢任何解决这个问题的想法

cheinan

I have a multiple web sits asp.net application.
In this application different domains using the same pages.
All pages inherit from base class named: PageBase
wich inherit from System.Web.UI.Page.
By using: HttpContext.Current.Request.ServerVariables["HTTP_HOST"]
i cen determine what is the domain and then get all the info i need
for this domain and everything is working good.

My problem begin when i want to use different visitor counter for each site based on session.
Because Global.asax have Global events:
Session_Start
Session_End
simple counter will count all visitors on all sites together.
I try to make code behind for the Global.asax in different class
but i cold not get in that class the PageBase(System.Web.UI.Page) web site info.

I will be very thankful for any ideas to solve this problem

cheinan

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

只是在用心讲痛 2024-09-14 10:14:52

我假设您能够在同一会话中从一个“站点”浏览到另一个“站点”,并且只创建一个会话。

在这种情况下,您需要将以下内容添加到会话中:

    Session["CountedHosts"] = new List<string>();

然后,在基页上添加以下内容:

var host = Request.ServerVariables["HTTP_HOST"];
var countedHosts = Session["CountedHosts"] as List<string>;
if (countedHosts != null && !countedHosts.Contains(host))
{
    countedHosts.Add(host);
}

然后在会话结束时,记录访问过的每个主机。

var countedHosts = Session["CountedHosts"] as List<string>;
if (countedHosts != null)
{
     foreach (var host in countedHosts)
     {
          //Log it

      }
}

I am assuming that you are able to browse from one "site" to the other within the same session and that there is only one session created.

In this case, you need to add the following to your Session:

    Session["CountedHosts"] = new List<string>();

Then, on your base page, add the following:

var host = Request.ServerVariables["HTTP_HOST"];
var countedHosts = Session["CountedHosts"] as List<string>;
if (countedHosts != null && !countedHosts.Contains(host))
{
    countedHosts.Add(host);
}

Then on session end, record each host that was visited.

var countedHosts = Session["CountedHosts"] as List<string>;
if (countedHosts != null)
{
     foreach (var host in countedHosts)
     {
          //Log it

      }
}
囚我心虐我身 2024-09-14 10:14:52

我无法在同一会话中从一个“站点”浏览到另一个“站点”,每个站点都在

创建的不同会话上。

但我非常感谢你,因为你给了我一个想法
如何解决这个问题

这就是我所做的:
我用字典“onlineList”创建了计数器类,我为每个站点自动创建一个密钥:

public abstract class counter{
public static Dictionary<string, int> onlineList = new Dictionary<string, int>();

 //do add one count
public static void doSiteCountOn(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] += 1;
    }
    else
    {
        onlineList.Add(siteID, 1);
    }
}

//do less one count
public static void doSiteCountOff(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] -= 1;
    }
    else
    {
        onlineList.Add(siteID, 0);
    }
}

//get the count
public static string onlineCount(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        return onlineList[siteID].ToString();
    }
    else
    {
        return "0";
    }
}

//reset the count if needed
public static void resetCount(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] = 0;
    }
}}

在我的基本页面上,我检查是否有 Session["siteID"]
如果不是,我启动一个计数器类,将 1 添加到站点计数器:

if (Session["siteID"] == null){
Session["siteID"] = _siteID;
counter.doSiteCountOn(_siteID);}

最后在我的 Session_End 上,我少计数一个:

void Session_End(object sender, EventArgs e){
if (Session["siteID"] != null)
    {
        counter.doSiteCountOff(Session["siteID"].ToString());
    }}

感谢您的帮助
对我迟到的回复感到

抱歉

I am not able to browse from one "site" to the other within the same session each site have is on

different session created.

but i am very thankful to you because you gave me en idea
how to solv this problem

here is what i did:
i created counter class with Dictionary "onlineList" were i automatic creat for each site a key:

public abstract class counter{
public static Dictionary<string, int> onlineList = new Dictionary<string, int>();

 //do add one count
public static void doSiteCountOn(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] += 1;
    }
    else
    {
        onlineList.Add(siteID, 1);
    }
}

//do less one count
public static void doSiteCountOff(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] -= 1;
    }
    else
    {
        onlineList.Add(siteID, 0);
    }
}

//get the count
public static string onlineCount(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        return onlineList[siteID].ToString();
    }
    else
    {
        return "0";
    }
}

//reset the count if needed
public static void resetCount(string siteID)
{
    if (onlineList.ContainsKey(siteID))
    {
        onlineList[siteID] = 0;
    }
}}

on my base page i check if there is Session["siteID"]
and if not i start one and make my counter class to add 1 to the site counter:

if (Session["siteID"] == null){
Session["siteID"] = _siteID;
counter.doSiteCountOn(_siteID);}

and finaly on my Session_End i do one count less:

void Session_End(object sender, EventArgs e){
if (Session["siteID"] != null)
    {
        counter.doSiteCountOff(Session["siteID"].ToString());
    }}

thank for your halp
and sorry for my late respons

cheinan

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文