| px | top | add code | search | signup | login | help |
<?PHP
/* ----------------------------------------------------------------------------------------------
Simple text-counter by: Rikard Ronnkvist - riro@snowland.nu
I disclaim nothing...nor do I claim anything...
But it would be nice if you included this disclaimer...
-------------------------------------------------------------------------------------------------
MySql support by kucopile (kucopile@abv.bg)
Usage:
make $pg != 1 for mysql
added fromip field for preventing immidiate reloading of pages.
-------------------------------------------------------------------------------------------------
How-to:
Log in as postgres and run
createdb counter
psql counter
And at the postgree-prompt run:
CREATE TABLE COUNTER (PAGE text, LAST_ACCESS text, FIRST_ACCESS text, COUNT int, NS int, MSIE int, OTHER int);
REVOKE ALL ON COUNTER FROM nobody;
GRANT SELECT ON COUNTER TO nobody;
GRANT UPDATE ON COUNTER TO nobody;
How-to (MySql):
Log in as postgres and run
create database counter
use counter
And at the postgree-prompt run:
create table counter (page text, last_access text, first_access text, count int, ns int, msie int, other int, fromip text);
REVOKE ALL ON COUNTER FROM nobody;
GRANT SELECT ON COUNTER TO nobody;
GRANT UPDATE ON COUNTER TO nobody;
From your php-page use:
<?PHP include(\'counter.php3\'); ?>
and you will get a string like:
Hit: 42 | Since: 99-09-01 | Last 99-09-08 | Internet Explorer 25% | Netscape 65% | Other 10%
(You might want to change the $datestr and $timestr to your local date-string)
---------------------------------------------------------------------------------------------- */
//
$pg = 0xff;
$username = "test\";
$password = \"test\";
$databasename = \"counter\"; // Watch the case !
$tablename = \"counter\";
// procents
function counterp( $totalcnt, $browsercnt ) {
if ($browsercnt != 0) {
return ($browsercnt / $totalcnt) * 100;
} else {
return 0;
}
}
$datestr = date(\"y-m-d\");
$timestr = date(\"H:i\");
$agent = strtoupper($HTTP_USER_AGENT);
$browser = \"OTHER\";
if (eregi(\"OZILLA\",$agent)) $browser = \"NS\";
if (eregi(\"MSIE\" ,$agent)) $browser = \"MSIE\";
$script = getenv(\"SCRIPT_FILENAME\");
if ($pg==1) {
$cntrconn = pg_connect(\"localhost\", \"5432\", \"\", \"\", $databasename);
} else {
$cntrconn = mysql_pconnect(\"localhost\" ,$username, $password);
}
if (!$cntrconn) {
echo \"<b>An error occurred. Connect fail</b>\\n\";
exit;
}
$sqlcmd = \"SELECT * FROM $tablename WHERE PAGE = \'\" . $script . \"\';\";
if ($pg==1) {
$result = pg_Exec($cntrconn, $sqlcmd);
} else {
$result = mysql_db_query($databasename,$sqlcmd);
}
if (!$result) {
echo \"<B>An error occurred. Database not found!?</b>\\n\";
exit;
}
if ($pg==1) {
$num = pg_NumRows($result);
} else {
$num = mysql_num_rows($result);
}
if ($num == 0) {
$sqlcmd = \"INSERT INTO $tablename (PAGE, COUNT, LAST_ACCESS, FIRST_ACCESS, NS, MSIE, OTHER) VALUES (\'\" . $script . \"\', 1, \'\" . $timestr . \"\', \'\" . $datestr . \"\', 0, 0, 0);\";
if ($pg==1) {
$result = pg_Exec($cntrconn, $sqlcmd);
} else {
$result = mysql_db_query($databasename,$sqlcmd);
}
$sqlcmd = \"UPDATE $tablename SET \" . $browser . \" = \" . $browser . \" +1 WHERE PAGE = \'\" . $script . \"\';\";
if ($pg==1) {
$result = pg_Exec($cntrconn, $sqlcmd);
} else {
$result = mysql_db_query($databasename,$sqlcmd);
}
echo \"Hit: 1 | Since: \", $datestr, \" | Last: \", $timestr;
} else {
if ($pg==1) {
$msie = counterp(pg_Result($result, 0, \"count\"), pg_Result($result, 0, \"msie\"));
$ns = counterp(pg_Result($result, 0, \"count\"), pg_Result($result, 0, \"ns\"));
$other = counterp(pg_Result($result, 0, \"count\"), pg_Result($result, 0, \"other\"));
echo \"Hit: \", (pg_Result($result, 0, \"count\")+1), \" | \",
\"Since: \", pg_Result($result, 0, \"first_access\"), \" | \",
\"Last: \", pg_Result($result, 0, \"last_access\"), \" | \",
\"Internet Explorer: \", sprintf(\"%01.0f\", $msie), \"% | \",
\"Netscape: \", sprintf(\"%01.0f\", $ns), \"% | \",
\"Other: \", sprintf(\"%01.0f\", $other), \"%\";
} else { // pgre
$row = mysql_fetch_array($result);
$msie = counterp($row[\"count\"], $row[\"msie\"] );
$ns = counterp($row[\"count\"], $row[\"ns\"]);
$other = counterp($row[\"count\"], $row[\"other\"]);
$asd = $row[\"count\"]+1;
echo \"Hit: \", $asd, \" | \",
\"Since: \", $row[\"first_access\"], \" | \",
\"Last: \", $row[\"last_access\"], \" | \",
\"Internet Explorer: \", sprintf(\"%01.0f\", $msie), \"% | \",
\"Netscape: \", sprintf(\"%01.0f\", $ns), \"% | \",
\"Other: \", sprintf(\"%01.0f\", $other), \"%\";
}
global $REMOTE_ADDR;
$sqlcmd = \"UPDATE $tablename SET fromip=\'$REMOTE_ADDR\', COUNT = COUNT+1, LAST_ACCESS = \'\" . $timestr . \"\', \" . $browser . \" = \" . $browser . \" +1 WHERE PAGE = \'\" . $script . \"\';\";
if ($pg==1) {
$result = pg_Exec($cntrconn, $sqlcmd);
} else {
if ($row[\"fromip\"] != $REMOTE_ADDR )
$result = mysql_db_query($databasename,$sqlcmd);
}
}
if ($pg==1) {
pg_FreeResult($result);
pg_Close($cntrconn);
} else {
}
?>
Comments or questions?
PX is running PHP 5.2.11
Thanks to Miranda Productions for hosting and bandwidth.
Use of any code from PX is at your own risk.