I set this up using a text file but you'll get the idea save the the
last ip in a field in your database and if the current ip matches the
last one then its the same person and no count is registered. I already
now this works because I'm using it.
$ip = getenv("REMOTE_ADDR");
if ($ip != $data[6]) {
$dstamp = date(Ymd);
$wstamp = date(Ymd,mktime(0,0,0,date("m"),date("d")+(7 -
date(w)),date("Y")));
if ($data[4] < $dstamp) {
$data[2] = 0; $data[4] = $dstamp;
}
if ($data[5] < $wstamp) {
$data[3] = 0; $data[5] = $wstamp;
}
$data[1]++; $data[2]++; $data[3]++;
$fp = fopen($file, "w+");
flock($fp, 2);
fputs($fp,
"$data[0]|$data[1]|$data[2]|$data[3]|$data[4]|$data[5]|$ip");
flock($fp, 3);
fclose($fp);
}
Gavin Clark wrote:
>
> on 5/31/00 11:24 PM, Vincent Danen wrote:
>
> > Sorry, probably wrong list but... I need a little help with PHP. I'm
> > trying to make a PHP-based counter for my web page but want it based on
> > unique visits (not refreshes, etc.)
> >
> > I'm storing the IP address in the MySQL table and want to compare the
> > current IP to the last IP address stored in the database, so this is what
> > I'm currently using (but it doesn't work):
> >
> > $curip = getenv('REMOTE_ADDR');
> > if ($ip != $curip) {
> > $count = $count + 1;
> > }
> >
> > But it increases on every refresh. I don't see a different comparison
> > operator for strings (like in perl), otherwise I'd use "ne" instead of
> > "!=". Does anyone know why this isn't working?
> >
> > Thanks in advance.
>
> "!=" is right. try looking at the data to see what you've really got.
>
> <?
> $curip = getenv('REMOTE_ADDR');
> if ($ip != $curip) {
> $count = $count + 1;
> }
> echo '$ip= ' . $ip;
> echo ' $curip= ' . $curip;
> ?>
>
> also - do you really want to store all those ip addresses for a simple hit
> counter? that table could get pretty big and you wouldn't count visitors who
> come via proxy servers and gateways (everyone from AOL would count as one
> hit) . I'd give the visitor a cookie and increment only if the cookie is not
> present. I use this technique to keep people from voting twice in my poll.
> the count is a little sloppy because lots of people turn cookies off but
> most people leave cookies on and in fact have never heard of them.
>
> cookies are easy:
>
> // test for cookie
> if (!$hit) {
> //increment counter
> $count++;
> //set hit cookie
> setcookie("hit","Been there!",time() +3600,"/"); /* expire in 1 hour */
> }
>
> Gavin