<?

/*
This counter prints the number of visitors and the number of distinct visitors.
It also prints the number of visitors this hour and the number of visitors today.
For this one, it's better (because we want the number of visitors today and this 
hour) to use the unix timestamp. We get that by using the function time().

This database table for this counter stores the IP address of the visitor, the formatted date at which the 
visit took place and the unix timestamp at which the visit took place.

Note that to use this counter, you need to include the following statement in 
the "php" page in which you want to include the counter:
include("counter_v2.php");
The page in which you include this has to be a PHP page. You'll have to assign a value for 
the variable $page in counter_multi_pages.php in the include file (not the included one). But, 
you can have an htaccess file to make it work in other types
of documents. You can also use server side includes (SSI) commands 
and include it in an HTML file. In this case, make the extension of the HTML 
file .shtml (instead of .html or .htm) and include the following statement in 
the place where you want the number of visitors to be printed:

<!--#include virtual="counter_v2.php"-->

If the file you're including is located in another directory, you can use it as follows:
E.g. assume that the PHP file is located in www/php and the shtml file in which you're including
the PHP file is located in www/teaching/Fall_06/121

<!--#include virtual="../../../php/counter_v2.php"-->

*/


$ip = $_SERVER['REMOTE_ADDR'];
$formatted_date = date('j F Y, H:i:s T');
$date = getdate();
$current_year = $date['year'];
$current_mon = $date['mon'];
$current_mday = $date['mday'];
$current_hour = $date['hours'];
$time = time();

@$d = mysql_connect('localhost','database_username','database_password') or die("Can't connect");

@mysql_select_db('counter') or die();

$q = "insert into counter_v2 (ip,formatted_date,date) values (\"$ip\",\"$formatted_date\",\"$time\")";
@mysql_query($q) or die();

$q = "select ip, date from counter_v2";
@$r = @mysql_query($q) or die();
$n = mysql_num_rows($r);
$hour_count = 0;
$day_count = 0;
while ($row = mysql_fetch_row($r))
{
		$past_date = $row[1];
		@$date = getdate($past_date);
		$past_year = $date['year'];
		$past_mon = $date['mon'];
		$past_mday = $date['mday'];
		$past_hour = $date['hours'];
		if ($current_year == $past_year 
			&& $current_mon == $past_mon
			&& $current_mday == $past_mday)
		{
			$day_count++;
			if ($current_hour == $past_hour)
				$hour_count++;
		}	
}
$q = "select min(date) from counter_v2";
@$r = @mysql_query($q) or die();
@$row = mysql_fetch_row($r);
@$min_date = date('j F Y',$row[0]);

echo "<center>", $n, " visitors since $min_date.</center><BR>";
echo "<center>", $day_count, " visitors today.</center><BR>";
echo "<center>", $hour_count, " visitors this hour.</center><BR>";

$q = "select distinct ip from counter_v2";
@$r = @mysql_query($q) or die();
$n = mysql_num_rows($r);
echo "<center>", $n, " distinct visitors since Oct 29, 06.</center><BR>";

@mysql_close($d); 
?>
