Ok - neue Version:
PHP-Code:
<?php
///////////////////////////////////////////////////////////////////////////////////////
// Copyright (C) 2009 Dominik Bischoff
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation version 2 of the License.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
///////////////////////////////////////////////////////////////////////////////////////
// This script provides a counter for a webpage. Reloads from the same
// person are blocked for 24 hours by giving the person a cookie.
// counter save filename
$filename = "counter.txt";
// file structure
// ------------------------------
// mmddyyyy TotalToday TotalUntilThatDay
// mmddyyyy TotalToday Total
// ------------------------------
// actual date
$ddmmyy = date("dmY");
// show errors
error_reporting(E_ALL);
ini_set("display_errors", 1);
// read IP address
$ip = getenv("REMOTE_ADDR");
// set cookie with
// name : "beenhere"
// content : the ip address of the user
// expiration date : the actual time plus 24 hours
// !!! no 'echo' before setcookie() -> produces error !!!
setcookie("beenhere", $ip, time()+24*3600);
// test if cookie is already set
if(isset($_COOKIE['beenhere']))
{
// echo "already visited <br>";
}
// case: cookie not set => new visitor
else
{
// echo "new visitor <br>";
// check if file is 'not existing' OR 'available and writeable'
if(!file_exists($filename) || is_writeable($filename))
{
// create file handle and check if file can be opened
// 'r+' : read and write, set file pointer to begin of file
if ($fp = fopen($filename, 'r+'))
{
// save actual time: Do not wait longer than a second
// to avoid having to many open scripts
$startTime = microtime();
do {
// check if this script can get a lock on the desired file
$lockObtained = flock($fp, LOCK_EX);
// if no lock obtained, wait random time between 0 and 50 milliseconds
if(!$lockObtained)
{
usleep(round(rand(0, 50)*1000));
}
// if no lock obtained, repeat until lock obtained a time passed
} while ( (!$lockObtained) and ((microtime()-$startTime) < 1000));
// check if the file is locked
if ($lockObtained)
{
$lines = '';
// read whole file
// each array entry is a line
$lines = file($filename);
// file is now locked!
// 1 for todays date already in file
$founddate = 0;
// actual total of visitors
$actualtotal = 0;
foreach ($lines as $line_num => $line)
{
// split stream by " "
$words = explode(" ",$line);
// check if enough values in array
// check if second and third value are numerics
if(count($words)>2 && is_numeric($words[1]) && is_numeric($words[2]))
{
// save latest total
$actualtotal = $words[2];
// check if read out date is equal to todays date
$same = strcmp($ddmmyy,$words[0]);
// date match
if($same == 0)
{
// increment both counters and then write into file
fwrite($fp, $words[0]." ".($words[1]+1)." ".($words[2]+1)." \n");
$founddate = 1;
}
else
{
// date does not match - just write original line back into file
fwrite($fp, $line);
}
}
// pattern does not match - write line back
else
{
fwrite($fp, $line);
}
}
// todays date not yet in file: write new line
if($founddate == 0)
{
fwrite($fp, $ddmmyy." 1 ".($actualtotal+1)." \n");
}
}
else
{
// echo "couldn't get a lock on file - $filename";
}
fclose($fp);
}
else
{
// echo "$filename - fopen=false <br>";
}
}
else
{
// echo "$filename - is_writeable=false <br>";
}
}
?>
MfG Bischi
Lesezeichen