 |
/*
############################################
# #
# xCount #
# *config area* #
# #
############################################
# #
# Damit die Funktion von xCount #
# gewährleistet ist, müssen zunächst #
# folgende Variablen angepasst werden. #
# Copyright2002 by xScripts #
############################################
*/
/****
config
****/
// if you have a mysql-database, leave the line as it is
// if not, change it to $mode="file";
$mode = "mysql";
// mysql connection- and authentication-data
// ignore these lines, if you don't have mysql
$server = "localhost"; // mysql server
$database = "counter"; // mysql database
$user = "rennkuck"; // mysql user
$pass = "elster"; // mysql password
// mysql table names
// ignore these lines, if you don't have mysql
$ctable = "rcounter"; // table for the visit-counts
$iptable = "rcounterip"; // table to store the visiting ip-adresses
// datafile names
// ignore these lines if you run the script with mysql
$cfile = "rcounter.txt"; // file for the visit-counts
$ipfile = "rcounterip.txt"; // file to store the visiting ip-adresses
// now is the time to configure the output
// if you want just a text-mode counter, take the following
$showstring="<%x%>";
// if you rather want graphical output and have the pictures
// for each digit in, lets say, pics/0.gif, pics/1.gif, etc. then take this:
//$showstring="
.gif\" width=\"10\" height=\"22\" border=\"0\" alt=\"<%x%>\">";
/****
init
****/
// ensure that the mode is correct
$mode=strtolower($mode); // to lower case
if ($mode!="mysql") $mode="file"; // either 'mysql' or 'file' - nothing else
// check the existence of the necessary files/tables
if ($mode=="file")
{
if (!file_exists($cfile))
{ $fp=fopen($cfile,"w"); if ($fp) fclose($fp); else { echo "-1"; exit; } }
if (!file_exists($ipfile))
{ $fp=fopen($ipfile,"w"); if ($fp) fclose($fp); else { echo "-2"; exit; } }
}
else // mode==mysql
{
// connect to database
$conn=mysql_connect($server,$user,$pass); // connect to the database-server ans login
mysql_select_db($database,$conn); // select database
// create the tables
$sql="CREATE TABLE IF NOT EXISTS $ctable
( id int(11) DEFAULT '0' NOT NULL AUTO_INCREMENT,
page text NOT NULL,
count int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (id)
)";
mysql_query($sql,$conn);
$sql="CREATE TABLE IF NOT EXISTS $iptable
( id int(11) DEFAULT '0' NOT NULL AUTO_INCREMENT,
ip varchar(30) NOT NULL,
page text NOT NULL,
stamp int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (id)
)";
mysql_query($sql,$conn);
}
// set the page-variable, if it is not set yet
if (!isset($page) || ($page=="")) $page=$PHP_SELF;
// eliminate spaces from pagename - they're used as seperators in the files
$page=str_replace(" ","_",$page);
/****
...and action
****/
// IP check
$update=true; // first we assume, the guy wasn't here today
$date=getdate();
if ($mode=="file")
{
$newcontent=""; // this will be written back to the ipfile
$ips=file($ipfile); // read the ip-file
while (list($lnr,$line) = each($ips)) // extract each line
{
$col=explode(" ",$line); // split into the three parts
$vdate=getdate($col[2]); // the date that the ip from this line was on the page $col[1]
if ($vdate[mday] == $date[mday]) // ip $col[0] was on page $col[1] TODAY...
$newcontent.=$line; // so we leave it in the file! -> forget the ones from yesterday
if (($col[0]==$REMOTE_ADDR) && ($col[1]==$page)) // reload-button pressed ;)
$update=false;
}
// add a line for the current ip and page if necessary
if ($update)
$newcontent.="$REMOTE_ADDR $page ".time()."\n";
// now update the ipfile
$fp=fopen($ipfile,"w");
fwrite($fp,$newcontent);
fclose($fp);
}
else // mode==mysql
{
$sql = "SELECT * from $iptable"; // check all old ip-adresses
$result = mysql_query($sql,$conn);
while ($row=mysql_fetch_array($result)) // yes, all ;)
{
$vdate=getdate($row[stamp]); // the date of the visit
if ($vdate[mday] != $date[mday]) // if it was yesterday or earlier...
{
$sql = "DELETE from $iptable WHERE id='".$row[id]."'"; // ...erase it from the table
mysql_query($sql,$conn);
}
if (($row[ip]==$REMOTE_ADDR) && ($row[page]==$page)) // he, buddy was here today (reload-button?)
$update=false;
}
if ($update) // in case he wasn't here today yet...
{
$sql = "INSERT INTO $iptable (ip,page,stamp) VALUES ('$REMOTE_ADDR','$page','".time()."')";
mysql_query($sql,$conn); // ...we remember his visit
}
}
// get and update count data
if ($mode=="file")
{
$newcontent=""; // set it back (was used before for the ipfile above)
$data=file($cfile); // get the current file-content
while (list($lnr,$line) = each($data)) // and do something for each line
{
if (strlen($line) < 2) continue; // for the last line (which is empty)
$col=explode(" ",$line); // split by spaces
if ($col[0] != $page) // not the current page - remember and skip
$newcontent.=$line;
else // our current page
{
$count = $col[1]; // get the count
if ($update) $count = $count + 1; // increase, if the is new today
}
}
if (!isset($count) || ($count=="")) $count="1"; // for the very first time on this page
$newcontent.="$page $count\n"; // add the line for this page
// now update the data file
$fp=fopen($cfile,"w");
fwrite($fp,$newcontent);
fclose($fp);
}
else // mode==mysql
{
$sql = "SELECT * from $ctable WHERE page = '$page'"; // get the data for this page
$result = mysql_query($sql, $conn);
$daten = mysql_fetch_array($result);
if (empty($daten[page])) // ups, must be the first time, this page is hit
{
$sql = "INSERT INTO $ctable (page,count) VALUES ('$page', '1')"; // you are the number 1 :)
mysql_query($sql, $conn);
$count = 1;
} else {
$count = $daten[count];
if ($update) $count = $count + 1;
$sql = "UPDATE $ctable SET count='$count' WHERE page='$page'"; // remember the current value
mysql_query($sql,$conn);
}
}
// draw the count data
// "heavy" mathematics here *gg*
$tenpot=10;
// start with the leftmost digit
while ($tenpot <= $count) $tenpot *= 10;
while ($tenpot > 1)
{
$tenpot /= 10;
// get the leftmost digit
$ziff = $count / $tenpot; // 274/100=2, 74/10=7, 4/1=4 - voila
settype($ziff,integer); // not sure if this is necessary
// write it out
echo str_replace("<%x%>",$ziff,$showstring);
// forget the leftmost digit
$count %= $tenpot;
}
/****
ending
****/
//prepare to count something different on the same page
unset($page);
?>
/
/*
############################################
# #
# xCount #
# *config area* #
# #
############################################
# #
# Damit die Funktion von xCount #
# gewährleistet ist, müssen zunächst #
# folgende Variablen angepasst werden. #
# Copyright2002 by xScripts #
############################################
*/
/****
config
****/
// if you have a mysql-database, leave the line as it is
// if not, change it to $mode="file";
$mode = "mysql";
// mysql connection- and authentication-data
// ignore these lines, if you don't have mysql
$server = "localhost"; // mysql server
$database = "counter"; // mysql database
$user = "rennkuck"; // mysql user
$pass = "elster"; // mysql password
// mysql table names
// ignore these lines, if you don't have mysql
$ctable = "rcounter"; // table for the visit-counts
$iptable = "rcounterip"; // table to store the visiting ip-adresses
// datafile names
// ignore these lines if you run the script with mysql
$cfile = "rcounter.txt"; // file for the visit-counts
$ipfile = "rcounterip.txt"; // file to store the visiting ip-adresses
// now is the time to configure the output
// if you want just a text-mode counter, take the following
$showstring="<%x%>";
// if you rather want graphical output and have the pictures
// for each digit in, lets say, pics/0.gif, pics/1.gif, etc. then take this:
//$showstring="
.gif\" width=\"10\" height=\"22\" border=\"0\" alt=\"<%x%>\">";
/****
init
****/
// ensure that the mode is correct
$mode=strtolower($mode); // to lower case
if ($mode!="mysql") $mode="file"; // either 'mysql' or 'file' - nothing else
// check the existence of the necessary files/tables
if ($mode=="file")
{
if (!file_exists($cfile))
{ $fp=fopen($cfile,"w"); if ($fp) fclose($fp); else { echo "-1"; exit; } }
if (!file_exists($ipfile))
{ $fp=fopen($ipfile,"w"); if ($fp) fclose($fp); else { echo "-2"; exit; } }
}
else // mode==mysql
{
// connect to database
$conn=mysql_connect($server,$user,$pass); // connect to the database-server ans login
mysql_select_db($database,$conn); // select database
// create the tables
$sql="CREATE TABLE IF NOT EXISTS $ctable
( id int(11) DEFAULT '0' NOT NULL AUTO_INCREMENT,
page text NOT NULL,
count int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (id)
)";
mysql_query($sql,$conn);
$sql="CREATE TABLE IF NOT EXISTS $iptable
( id int(11) DEFAULT '0' NOT NULL AUTO_INCREMENT,
ip varchar(30) NOT NULL,
page text NOT NULL,
stamp int(11) DEFAULT '0' NOT NULL,
PRIMARY KEY (id)
)";
mysql_query($sql,$conn);
}
// set the page-variable, if it is not set yet
if (!isset($page) || ($page=="")) $page=$PHP_SELF;
// eliminate spaces from pagename - they're used as seperators in the files
$page=str_replace(" ","_",$page);
/****
...and action
****/
// IP check
$update=true; // first we assume, the guy wasn't here today
$date=getdate();
if ($mode=="file")
{
$newcontent=""; // this will be written back to the ipfile
$ips=file($ipfile); // read the ip-file
while (list($lnr,$line) = each($ips)) // extract each line
{
$col=explode(" ",$line); // split into the three parts
$vdate=getdate($col[2]); // the date that the ip from this line was on the page $col[1]
if ($vdate[mday] == $date[mday]) // ip $col[0] was on page $col[1] TODAY...
$newcontent.=$line; // so we leave it in the file! -> forget the ones from yesterday
if (($col[0]==$REMOTE_ADDR) && ($col[1]==$page)) // reload-button pressed ;)
$update=false;
}
// add a line for the current ip and page if necessary
if ($update)
$newcontent.="$REMOTE_ADDR $page ".time()."\n";
// now update the ipfile
$fp=fopen($ipfile,"w");
fwrite($fp,$newcontent);
fclose($fp);
}
else // mode==mysql
{
$sql = "SELECT * from $iptable"; // check all old ip-adresses
$result = mysql_query($sql,$conn);
while ($row=mysql_fetch_array($result)) // yes, all ;)
{
$vdate=getdate($row[stamp]); // the date of the visit
if ($vdate[mday] != $date[mday]) // if it was yesterday or earlier...
{
$sql = "DELETE from $iptable WHERE id='".$row[id]."'"; // ...erase it from the table
mysql_query($sql,$conn);
}
if (($row[ip]==$REMOTE_ADDR) && ($row[page]==$page)) // he, buddy was here today (reload-button?)
$update=false;
}
if ($update) // in case he wasn't here today yet...
{
$sql = "INSERT INTO $iptable (ip,page,stamp) VALUES ('$REMOTE_ADDR','$page','".time()."')";
mysql_query($sql,$conn); // ...we remember his visit
}
}
// get and update count data
if ($mode=="file")
{
$newcontent=""; // set it back (was used before for the ipfile above)
$data=file($cfile); // get the current file-content
while (list($lnr,$line) = each($data)) // and do something for each line
{
if (strlen($line) < 2) continue; // for the last line (which is empty)
$col=explode(" ",$line); // split by spaces
if ($col[0] != $page) // not the current page - remember and skip
$newcontent.=$line;
else // our current page
{
$count = $col[1]; // get the count
if ($update) $count = $count + 1; // increase, if the is new today
}
}
if (!isset($count) || ($count=="")) $count="1"; // for the very first time on this page
$newcontent.="$page $count\n"; // add the line for this page
// now update the data file
$fp=fopen($cfile,"w");
fwrite($fp,$newcontent);
fclose($fp);
}
else // mode==mysql
{
$sql = "SELECT * from $ctable WHERE page = '$page'"; // get the data for this page
$result = mysql_query($sql, $conn);
$daten = mysql_fetch_array($result);
if (empty($daten[page])) // ups, must be the first time, this page is hit
{
$sql = "INSERT INTO $ctable (page,count) VALUES ('$page', '1')"; // you are the number 1 :)
mysql_query($sql, $conn);
$count = 1;
} else {
$count = $daten[count];
if ($update) $count = $count + 1;
$sql = "UPDATE $ctable SET count='$count' WHERE page='$page'"; // remember the current value
mysql_query($sql,$conn);
}
}
// draw the count data
// "heavy" mathematics here *gg*
$tenpot=10;
// start with the leftmost digit
while ($tenpot <= $count) $tenpot *= 10;
while ($tenpot > 1)
{
$tenpot /= 10;
// get the leftmost digit
$ziff = $count / $tenpot; // 274/100=2, 74/10=7, 4/1=4 - voila
settype($ziff,integer); // not sure if this is necessary
// write it out
echo str_replace("<%x%>",$ziff,$showstring);
// forget the leftmost digit
$count %= $tenpot;
}
/****
ending
****/
//prepare to count something different on the same page
unset($page);
?>