Beschreibung:
Dieser Snippet führt nebenbei auch File Statistiken. Ihr könnt also sehen wieviele User auf welcher Seite waren indem ihr folgende Datei aufruft:
~/counter/[aspxfilename].txt
Beispiel zur deklaration:
Importieren:
using PageCounting;
In den Page Load:
PageCounter PCounter = new PageCounter();
PCounter.CountThisPage();
Auslesen der aktuellen Hits:
<%= new PageCounting.PageCounter().getCounts() %>
oder
int akutelleHits = new PageCounting.PageCounter().getCounts();
Manche Server haben Probleme damit Ordner erstellen zu lassen, wie meiner (Sharinghoster).
Um das zu umgehen erstellt einfach im ordner, wo die Page liegt den Ordner "counter".
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace PageCounting
{
/// <summary>
/// Zusammenfassungsbeschreibung für PageCounter
/// </summary>
public class PageCounter
{
public PageCounter()
{
}
public void CountThisPage()
{
//
// Dateicheck
// Class File braucht Schreib und Änderrechte
//
string dir = HttpContext.Current.Server.MapPath("~/App_Code/counter");
string file = HttpContext.Current.Server.MapPath("~/App_Code/counter/counter.txt");
string file2 = HttpContext.Current.Server.MapPath("~/App_Code/counter/ips.txt");
//Wir führen außerdem eine Statistik über alle Dateien. Wann welche Aspx Datei besucht wurde
string FileNameForAspx = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.Path);
string RawFile = HttpContext.Current.Server.MapPath("~/App_Code/counter/" + FileNameForAspx + ".txt");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
if (!File.Exists(file))
{
File.Create(file);
}
if (!File.Exists(file2))
{
File.Create(file2);
}
if (!File.Exists(RawFile))
{
File.Create(RawFile);
}
FileInfo FI = new FileInfo(file2);
if (FI.LastWriteTime.Day != DateTime.Now.Day) {
File.WriteAllText(file2, "");
}
string TotalCounter = HttpContext.Current.Server.MapPath("~/App_Code/counter/counter.txt");
string FileCounter = HttpContext.Current.Server.MapPath("~/App_Code/counter/" + FileNameForAspx + ".txt");
string myIp = HttpContext.Current.Request.UserHostAddress.ToString();
if (!IP_Exists())
{
string content_total = File.ReadAllText(TotalCounter);
File.WriteAllText(TotalCounter, content_total + "" + myIp + ";");
File.WriteAllText(file2, content_total + "" + myIp + ";");
string content_raw = File.ReadAllText(FileCounter);
File.WriteAllText(FileNameForAspx, content_raw + "" + myIp + ";");
}
}
private bool IP_Exists()
{
bool ret = false;
string[] ips = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Code/counter/ips.txt")).Split(';');
string myIp = HttpContext.Current.Request.UserHostAddress.ToString();
for (int x = 0; x < ips.Length; x++)
{
if (myIp == ips[x]) ret = true;
}
return ret;
}
public int getCounts()
{
int ret = 0;
try
{
ret = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Code/counter/counter.txt")).Split(';').Length - 1;
}
catch
{
//wenn ein undeutiger Wert rauskommt (Oben) gebe 0 aus. Da dies jedoch schon der Default Wert ist brauchen wir im Catch keinen Code
}
return ret;
}
}
}
Schlagwörter: Counter, Hit, Pagecounter, Ip Sperre, Asp.net
Hallo,
ein paar Anmerkungen dazu:
1.
Du solltest darüber nachdenken, die gesperrten IPs nach einer bestimmten Zeit wieder freizugeben. Denn bei dynamischen IPs zählst Du sonst immer nur den ersten Aufruf, obwohl es sich einige Zeit später um einen völlig anderen Besucher handeln könnte, der mittlerweile diese IP erhalten hat.
Es ist durchaus üblich, jede IP-Adressen einmal am Tag zählen zu lassen - das würde aber bei Deinem Snippet wenn ich's richtig sehe einige Umbauarbeiten nach sich ziehen 🤔
2.
Um das zu umgehen erstellt einfach im ordner, wo die Page liegt den Ordner "counter". Günstiger wäre IMHO, den Unterordner nach
~/App_Data
zu legen. Da besteht auch normalerweise Schreibrecht um den Ordner automatisch anzulegen.
Gruß, MarsStein
Non quia difficilia sunt, non audemus, sed quia non audemus, difficilia sunt! - Seneca
haste recht...kleiner denkfehler ABER....ändert sich nicht fast jede IP um 0uhr?
ändert sich nicht fast jede IP um 0uhr?
Bei festen IPs (wie in vielen Firmen) ändert sie sich gar nicht. Der Zeitpunkt wann sich IPs ändern ist völlig unterschiedlich. Aber es werden immer wieder die gleichen IPs an andere Nutzer verteilt.
Gruß, MarsStein
Non quia difficilia sunt, non audemus, sed quia non audemus, difficilia sunt! - Seneca
Geändert (änder ich oben auch nochmal):
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.IO;
namespace PageCounting
{
/// <summary>
/// Zusammenfassungsbeschreibung für PageCounter
/// </summary>
public class PageCounter
{
public PageCounter()
{
}
public void CountThisPage()
{
//
// Dateicheck
// Class File braucht Schreib und Änderrechte
//
string dir = HttpContext.Current.Server.MapPath("~/App_Code/counter");
string file = HttpContext.Current.Server.MapPath("~/App_Code/counter/counter.txt");
string file2 = HttpContext.Current.Server.MapPath("~/App_Code/counter/ips.txt");
//Wir führen außerdem eine Statistik über alle Dateien. Wann welche Aspx Datei besucht wurde
string FileNameForAspx = Path.GetFileNameWithoutExtension(HttpContext.Current.Request.Path);
string RawFile = HttpContext.Current.Server.MapPath("~/App_Code/counter/" + FileNameForAspx + ".txt");
if (!Directory.Exists(dir))
{
Directory.CreateDirectory(dir);
}
if (!File.Exists(file))
{
File.Create(file);
}
if (!File.Exists(file2))
{
File.Create(file2);
}
if (!File.Exists(RawFile))
{
File.Create(RawFile);
}
FileInfo FI = new FileInfo(file2);
if (FI.LastWriteTime.Day != DateTime.Now.Day) {
File.WriteAllText(file2, "");
}
string TotalCounter = HttpContext.Current.Server.MapPath("~/App_Code/counter/counter.txt");
string FileCounter = HttpContext.Current.Server.MapPath("~/App_Code/counter/" + FileNameForAspx + ".txt");
string myIp = HttpContext.Current.Request.UserHostAddress.ToString();
if (!IP_Exists())
{
string content_total = File.ReadAllText(TotalCounter);
File.WriteAllText(TotalCounter, content_total + "" + myIp + ";");
File.WriteAllText(file2, content_total + "" + myIp + ";");
string content_raw = File.ReadAllText(FileCounter);
File.WriteAllText(FileNameForAspx, content_raw + "" + myIp + ";");
}
}
private bool IP_Exists()
{
bool ret = false;
string[] ips = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Code/counter/ips.txt")).Split(';');
string myIp = HttpContext.Current.Request.UserHostAddress.ToString();
for (int x = 0; x < ips.Length; x++)
{
if (myIp == ips[x]) ret = true;
}
return ret;
}
public int getCounts()
{
int ret = 0;
try
{
ret = File.ReadAllText(HttpContext.Current.Server.MapPath("~/App_Code/counter/counter.txt")).Split(';').Length - 1;
}
catch
{
//wenn ein undeutiger Wert rauskommt (Oben) gebe 0 aus. Da dies jedoch schon der Default Wert ist brauchen wir im Catch keinen Code
}
return ret;
}
}
}