I'm trying to code a hit counter, and I'm having trouble getting it to differentiate between unique and regular hits. I'm using a MySQL interface. One field references all the IP addresses for the day, in format of ip|ip|ip|ip... etc. I'm trying to do an if (!in_array [array created by explode |]) go on to add to the unique hits. It's not quite working however. It doesn't seem to recognize the IP address in the array, so it keeps adding it to it again and again. Well, here's the code. Please help if you can...
<?php
/*************************************
* Dissionance Counter v0.1 *
* by Nite Phire *
*====================================*
* [url]http://dissonance.ketsukaiten.net/[/url] *
* [email]nitephire@ketsukaiten.net[/email] *
*************************************/
// MySQL information, edit this.
$mysqlhost = 'localhost';
$mysqluser = 'xxxxxxx';
$mysqlpassword = 'xxxxxxx';
$database = 'ketsuka_stats';
$tableprefix = 'test';
//
// DO NOT EDIT until the bottom when it says to!
//
// Connects to MySQL.
$connect = mysql_connect ($mysqlhost, $mysqluser, $mysqlpassword)
or die ("Wrong host, username, or password.");
mysql_select_db ($database)
or die ("Could not select database.");
// Initial retrieval of information.
$hitsquery = mysql_query ('SELECT * FROM ' . $tableprefix . '_stats WHERE type="hits";')
or die ('Could not retrieve original hits information.');
$hitsarray = mysql_fetch_assoc ($hitsquery)
or die ('Could not sort hits information into array.');
$uniquequery = mysql_query ('SELECT * FROM ' . $tableprefix . '_stats WHERE type="unique";')
or die ('Could not retrieve original unique information.');
$uniquearray = mysql_fetch_assoc ($uniquequery)
or die ('Could not sort unique information into array.');
// Total hits.
$hitstotal = $hitsarray['total'];
$hitstotal = $hitstotal+1;
mysql_query ('UPDATE ' . $tableprefix . '_stats SET total="' . $hitstotal . '" WHERE type="hits";')
or die ('Could not update total hits.');
// Hits today and hits average.
$checkdate = $hitsarray['checkdate'];
if ($checkdate == date (Ymd)) {
$hitstoday = $hitsarray['today'];
$hitstoday = $hitstoday+1;
mysql_query ('UPDATE ' . $tableprefix . '_stats SET today="' . $hitstoday . '" WHERE type="hits";')
or die ('Could not update hits today.');
} else {
$newdate = date (Ymd);
$hitsyesterday = $hitsarray['today'];
$hits2daysago = $hitsarray['yesterday'];
$hits3daysago = $hitsarray['2daysago'];
$hits4daysago = $hitsarray['3daysago'];
$hitsavg = ($hitsyesterday+$hits2daysago+$hits3daysago+$hits4daysago)/4;
mysql_query ('UPDATE ' . $tableprefix . '_stats SET checkdate="' . $newdate . '",
today="1",
yesterday="' . $hitsyesterday . '",
2daysago="' . $hits2daysago . '",
3daysago="' . $hits3daysago . '",
4daysago="' . $hits4daysago . '",
average="' . $hitsavg . '"
WHERE type="hits";')
or die ('Could not update the date check or hits for recent days.');
};
// Unique hits.
$currentip = $_SERVER['REMOTE_ADDR'];
if ($checkdate == date (Ymd)) {
$retrieveip = $uniquearray['ipaddress'];
$ipaddress = array (explode ('|', $retrieveip));
if (!in_array ($currentip, $ipaddress)) {
$uniquetotal = $uniquearray['total'];
$uniquetoday = $uniquearray['today'];
$uniquetotal = $uniquetotal+1;
$uniquetoday = $uniquetoday+1;
$newipaddress = ($retrieveip . "|" . $currentip);
mysql_query ('UPDATE ' . $tableprefix . '_stats SET total="' . $uniquetotal . '",
today="' . $uniquetoday . '",
ipaddress="' . $newipaddress . '"
WHERE type="unique";')
or die ('Could not update unique hits information.');
} else {
return;
}
} else {
$newdate = date (Ymd);
$uniquetotal = $uniquearray['total'];
$uniquetotal = $uniquetotal+1;
$uniqueyesterday = $uniquearray['today'];
$unique2daysago = $uniquearray['yesterday'];
$unique3daysago = $uniquearray['2daysago'];
$unique4daysago = $uniquearray['3daysago'];
$uniqueavg = ($uniqueyesterday+$unique2daysago+$unique3daysago+$unique4daysago)/4;
mysql_query ('UPDATE ' . $tableprefix . '_stats SET total="' . $uniquetotal . '",
checkdate="' . $newdate . '",
today="1",
yesterday="' . $uniqueyesterday . '",
2daysago="' . $unique2daysago . '",
3daysago="' . $unique3daysago . '",
4daysago="' . $unique4daysago . '",
average="' . $uniqueavg . '",
ipaddress="' . $currentip . '"
WHERE type="unique";')
or die ('Could not update the date check or unique hits for recent days.');
}
// Retrieves new data.
$disphitsquery = mysql_query ('SELECT * FROM ' . $tableprefix . '_stats WHERE type="hits";')
or die ('Could not retrieve original hits information.');
$disphitsarray = mysql_fetch_assoc ($disphitsquery)
or die ('Could not sort hits information into array.');
$dispuniquequery = mysql_query ('SELECT * FROM ' . $tableprefix . '_stats WHERE type="unique";')
or die ('Could not retrieve original unique information.');
$dispuniquearray = mysql_fetch_assoc ($dispuniquequery)
or die ('Could not sort unique information into array.');
// Sets new variables.
$hitstotal = $disphitsarray['total'];
$hitstoday = $disphitsarray['today'];
$hitsavg = $disphitsarray['average'];
$uniquetotal = $dispuniquearray['total'];
$uniquetoday = $dispuniquearray['today'];
$uniqueavg = $dispuniquearray['average'];
mysql_close ($connect);
//
// Edit these display settings however you'd like, though keep a back-up for if it doesn't work. If you need help, contact Nite Phire.
//
print '
Page Hits Total :: ' . $hitstotal . '<br />
Page Hits Today :: ' . $hitstoday . '<br />
Page Hits Average :: ' . $hitsavg . '<br />
<br />
Unique Hits Total :: ' . $uniquetotal . '<br />
Unique Hits Today :: ' . $uniquetoday . '<br />
Unique Hits Average :: ' . $uniqueavg;
?>