$counter = pageCounter();
echo 'Seitenaufrufe: ', (!$counter) ? 'Fehler' : $counter, '';
===== Code =====
/**
* Simple page hit counter based on SQLite.
* Counts every unique page hit based on the requesting IP.
* Requests of the same IP within 12 hours are not counted.
*
* @return int or false if problem with database occured
*/
function pageCounter ()
{
if (!file_exists('counter.db'))
{
if (is_writeable('.'))
{
$db = sqlite_open('counter.db');
if (!$db)
{
return false;
}
sqlite_query($db, '
CREATE TABLE counter (
counter BIGINT NOT NULL
)
');
sqlite_query($db, '
CREATE TABLE ips (
ip varchar(15) not null,
entered bigint not null
)
');
sqlite_query($db, '
INSERT INTO counter (counter) VALUES (0)
');
}
else
{
return false;
}
}
else
{
$db = sqlite_open('counter.db');
if (!$db)
{
return false;
}
}
sqlite_query($db, '
DELETE FROM ips
WHERE ('.time().' - entered) > '.(12*60*60)
);
$result = sqlite_query($db, '
SELECT COUNT(*) as num
FROM ips
WHERE ip = "'.sqlite_escape_string($_SERVER['REMOTE_ADDR']).'"
');
$row = sqlite_fetch_object($result);
if ($row->num == 0)
{
sqlite_query($db, '
INSERT INTO ips (ip, entered)
VALUES ("'.sqlite_escape_string($_SERVER['REMOTE_ADDR']).'", '.time().')
');
sqlite_query($db, '
UPDATE counter
SET counter = (counter + 1)
');
}
$result = sqlite_query($db, '
SELECT counter
FROM counter
');
$row = sqlite_fetch_object($result);
return $row->counter;
}